Skip to content

Instantly share code, notes, and snippets.

import MySQLdb
import time
def get_connection():
return MySQLdb.connect(host='localhost',
user='your_username',
passwd='your_password',
db='your_database')
def query(conn, sql):
python3 /Users/cop/Downloads/mypy-master/scripts/mypy --use-python-path core/api.py ()(master⚡)
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/importlib/util.py:3: note: In module imported here,
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/utils/module_loading.py:67: note: ... from here,
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/apps/config.py:6: note: ... from here,
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/apps/__init__.py:1: note: ... from here,
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/site-packages/django/__init__.py:13: note: ... from here,
core/models.py:1: note: ... from here,
core/api.py:8: note: ... from here:
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/importlib/_bootstrap.py:1140: error: No module named '_frozen_importlib_external'
/Users/cop/.pyenv/versions/3.5.0/lib/python3.5/importlib
@chintanop
chintanop / test_mypy.py
Created September 23, 2015 16:10
Test MyPy
class Test1:
def create(self, a:int, b:str) -> None:
print("hello")
class Test2:
def create(self):
""" No error or warning generated even if first argument is a string here"""
a = "abc" # should've complained here!
b = "def"
Test1().create(a, b)
@chintanop
chintanop / gist:d0c189c2eebd34ce6b68
Created July 4, 2014 20:56
Copy tables across databases in Python
def copy_table(self, table_name, source_conn, target_conn):
""" copies all records from table_name from source to target db """
scursor = source_conn.cursor()
tcursor = target_conn.cursor()
scursor.execute("SELECT * from "+table_name)
desc = scursor.description
columns = ",".join([col[0] for col in desc])
@chintanop
chintanop / dynamic_django_forms.py
Created October 12, 2012 15:01
semantic dynamic django forms
form_widget = forms.ChoiceField(label=disp_entity.label, widget=forms.CheckboxSelectMultiple(), required=disp_entity.is_required, choices=choices)
#We also created some custom widgets to display custom arbitrary HTML:
form_widget = forms.CharField(label='',widget=CustomHTMLWidget(attrs={'html':html}), required=False)
#Next we define a dynamic Django form by subclassing the original django.Forms then assign our custom display fields to this form.
class DynamicForm(forms.Form):
def __init__(self, display_fields):
@chintanop
chintanop / on_off_rule_widget.js
Created October 12, 2012 14:56
rule widget javascript
$(document).ready(function() {
$("#{{attrs.rule_webparam}}_li").hide();
{% ifequal attrs.init_state "visible" %}
$("#{{attrs.toggle}}_li").show();
$("#{{attrs.toggle}}_li").css("cssText","display:block !important;");
{% else %}
$("#{{attrs.toggle}}_li").hide();
$("#{{attrs.toggle}}_li").css("cssText","display:none !important;");
{% endifequal %}