Posts

Showing posts from December, 2015

csv to list in python

This is dead simple way to read a csv file and feed the data into a Python list: import csv with open('file.csv', 'rb') as f:     reader = csv.reader(f)     your_list = list(reader) print your_list # [['This is the first line', 'Line1'], # ['This is the second line', 'Line2'], # ['This is the third line', 'Line3']] Reference:   http://stackoverflow.com/questions/24662571/python-import-csv-to-list

Duplicate entry error with the Django's User Profile model

I wrote a User Profile model which extends the Django's User authentication model as following: class TeacherProfile (models.Model):     user = models.OneToOneField(User, related_name="teacher_profile", \                         related_query_name="profile")     teacher_number = models.CharField(max_length=255, blank=True, null=True) # Office     desc = models.CharField(max_length=255, blank=True, null=True) # Description     title = models.CharField(max_length=255, blank=True, null=True)     school = models.ForeignKey('School', null=True,                          related_name="steacher_profiles",                         related_query_name="steacher_profile",             )     def __unicode__(self): ...

Sort Python dictionaries by key

Even though dictionaries in Python are unordered, we can use this following technique to sort the dictionaries: >>> import collections >>> d = {2:3, 1:89, 4:5, 3:0} >>> od = collections.OrderedDict(sorted(d.items())) >>> od OrderedDict([(1, 89), (2, 3), (3, 0), (4, 5)]) And the best thing is we can still using that collection just like our old dictionary: >> od[1] 89 >>> for k, v in od.iteritems(): print k, v 1  89 2  3 3  0 4  5 Note: the only difference is that we use iteritems() instead of items(). But it will be the same in python 3 (items()) References:   http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key

For me

Another quote of the day

“A single person is missing for you, and the whole world is empty.” ― Joan Didion, The Year of Magical Thinking

Quote of the day

"One who wants to wear the crown, bears the crown." ~" The Heirs "

Question of the day

"What type of crown are you trying to wear?  Was it wealth, fame or love?" ~" The Heirs "

Django celery task's timezone error

For some reason when I access the celery's tasks page in the Django's admin (.../djcelery/taskstate/), I got this error: Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed? I have to do the following to get rid of the error: 1. Uncomment these settings: # CELERY_TIMEZONE = 'UTC' # CELERY_ENABLE_UTC = True 2. Add this under mysqld section in /etc/mysql/my.cnf : default-time-zone = "+07:00" 3. Run the following command (without sudo): $ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p  mysql 4. Restart mysql: $ sudo service mysql restart