Monday, 4 January 2010

Nested List Comprehension

I never really feel like i'm any good at using Python unless I am doing cool things with List Comprehension.

Here's a quick example of nested list comprehension I used today to create a new list from a list containing lists of three items

Visually:-

[['mostly cloudy (day)', 'cloudy3.png', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png', 'cloudy2_night.png'],
['partly cloudy (day)', 'cloudy2.png', 'cloudy2.png'],
....................................
['Sunny', 'sunny.png', 'sunny.png']]

To this:-

[['mostly cloudy (night)', 'cloudy3_night.png'],
['mostly cloudy (day)', 'cloudy3.png'],
['partly cloudy (night)', 'cloudy2_night.png'],
....................................
['Sunny', 'sunny.png']]

The code then:-

[[a[0],a[1]] for a in theList]

Friday, 4 December 2009

Sharing media for Xbox360 on Linux/Mac

Ok, so you can't use samba with Xbox360 and there is no-way I am gonna use Windows. Luckerly, after some dedicated google-ing I came across 2 solutions, both of which did work, but at present the uShare seems to be the tidiest.

http://netou.co.uk/?p=28 Tested on SuSE Linux 11.1
http://ubuntuforums.org/showthread.php?t=848144

http://code.google.com/p/ps3mediaserver/ Tested on Mac 10.5.8 PPC and SuSE Linux 11.1

Tuesday, 10 November 2009

Xbox Live firewall Rules

A google for xbox live firewall ports never gave me specific ports requirements so here they are as I have discovered them.

Inbound. (A Nat based router/firewall these will need direct port forwarding. On my Juniper I set up a VIP.)
88 UDP
3074 UDP
3074 TCP

Outbound (I locked the DNS lookups to my DNS servers only)
53 UDP (DNS)
53 TCP (DNS)
88 UDP
3074 UPD
80 TCP
443 TCP - This was not mentioned on the net but traffic was trying to get through this port.

Sunday, 11 October 2009

A Question Of Python Style

Having ended up almost solely with Python at the end of a journey of many languages I have learnt and used; style has been something important to me.

Python has pep8 which states somethings definitively such as:-

* Use 4 spaces per indentation level" (I was actually using 2 until now!)
* Limit all lines to a maximum of 79 characters.
* Separate top-level function and class definitions with two blank lines.
* Method definitions inside a class are separated by a single blank line.

...so things like naming styles are purely at the discretion of the user. Fair enough but i like to be consistent where I can with these things, and coming from a Java background I pretty much stick with a style I picked up from there. Here's some explaination. Things always seemed very well defined in the land of Sun!

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367
http://geosoft.no/development/javastyle.html

...and so
* a class is ClassName()
* method names are lookBehindYou()
* class attributes are as above

this one from the geosoft link above I like also
* "Abbreviations and acronyms should not be uppercase when used as name.
exportHtmlSource(); // NOT: exportHTMLSource();
openDvdPlayer(); // NOT: openDVDPlayer();"

I've not mentioned doc strings here, the use of pep8 tool, pylint or pyflakes or even given an example class, well I guess that's the beauty of blogging!

This is worth knowing though http://wiki.laptop.org/go/Python_Style_Guide oooh and removing trailing whitespace in vim... thats :%s/\s\+$//

Tuesday, 29 September 2009

Python function args

I rarely use anything beyond named parameters, but this post captures what you need to know.

http://kbyanc.blogspot.com/2007/07/python-aggregating-function-arguments.html

Wednesday, 23 September 2009

Formatting Output from MySQL prompt

I'm not the biggest fan of MySQL and on the occasions I do use it I always forgot how to get useful results from my queries. To get results paged to less and have them display vertically, you can do this

mysql> \P less
PAGER set to 'less'

mysql> select * from blahblah \G

Restore Mysql Database from the .frm .MYI and .MYD files

OK so you have a mysql database that you have recovered from a backup. However, whereas you would normally have a nice sql dump to deal with, on this occasion you only have the .frm .MYI and .MYD files. Can you rescue that table the client has deleted? Indeed you can!

#> cd [where you databases are kept, possibly /var/lib/mysql ]
#> mkdir [name of database to rescue]
copy the .frm .MYI and .MYD files into it.

make sure the permissions are ok. Something like this
#> chown -R mysql:mysql [dbname]
#> chmod -R 700 [dbname]

Now if you are lucky, you should be able to do this:-
#> mysql -u root databasename -e "SHOW TABLES;";
#> mysqldump -u root databasename tablename