Tag: code

GTK3 red to green progress bar in Python

I really needed a progress bar that changes from red to green as it progresses. Well, I made one. Though it doesn’t support labels as the built-in GTK ProgressBar, one can easily be added next to the bar, or (if it’s static) in a tooltip. Also, not removing self.bar would result in an actual red-to-green gradient, instead of a color-changing bar. (Though it should still be removed when the bar is reset.)


Reordering elements in a GTK Menu

Recently I’ve had the pleasure to play around with GTK from Python while working on a (by now not so) little app for KanColle. (Since that last update on github I’ve rewritten the whole thing from scratch, but that’s not the point now.) On the way, I encountered a quite annoying problem: the reorder_child method for gtk.Menu just doesn’t work. I use Python 2.7, gtk.gtk_version says that’s 2.24.20, and gtk.pygtk_version is 2.24.0.


#nowplaying button for the Ubuntu sound menu

Every now and then I get this urge to just post a #nowplaying tweet, and at such a time it’s always such a drag to type it in manually. So I thought I’d make a menu item in the Ubuntu sound menu that sends such a tweet automatically. It turned out not to be all so easy at all…


Python dict lambda mapping fun

Just now I had to insert a certain dictionary variable into a database, and i wanted to use sqlite’s named placeholder scheme to make things simple.
self.cur.executemany('insert into mytable(id, name, field1, field2) values(:id, :name, :foo, :bar)',data)
Now the problem was that this dict called data didn’t have keys “foo” and “bar”: {'id': 1, 'name': u'name', 'keys': [1, 2]}.
I’d need to map this into the shape of {'id': 1, 'name': u'name', 'foo':1, 'bar':2} to use in the database query. Also, i wanted to keep the whole thing in a single command for reasons unknown. In the end, I came up with the following: map(lambda y: y.update({'foo':y['keys'][0], 'bar':y.pop(u'keys')[1]}) or y, data)
This did the trick. What it does: updates y with the ‘foo’ key as needed, then adds the ‘bar’ key as well, while popping off the unnecessary ‘keys’ key. The “or y” needs to be there, because dict.update returns None, not the dict object.


The charm of code

The past few weeks i’ve had a few chances to look at code. Snippets showing the newest cutting edge features of PHP. A fun online JavaScript console one of my twitter friends made. A multifunctional IRC bot written in Python. Some fun pieces of Ruby code. My own archives from the Java class I took last semester.


That developer

I don’t know how i managed to, but last week when i was speed-deving the new homepage for my club, i actually wrote it in a way that proved to be very efficient and with this i could avoid the biggest problem of developing the blog engine. That is, i simply couldn’t concentrate long enough, because after a while some weird problems started popping up from the MVC separation and the OOP, and at the same time it took enormous effort to get any output from the template.

With this new way i not only keep the whole thing more or less completely OOP, but i think MVC are separated well enough as well. Not to mention that the way i’m handling the nesting and inheritance of objects, i can get output from it in a very early stage, which makes debugging easier, lets me see design problems early on and also motivates me to actually finish the code. Which means that this might be the first time in god only knows how many attempts these past six years to rewrite the blog engine that i could get it done.

I’m so excited.


Nyilván

Pont akkor jön a megvilágosodás, hogy hogy lehetne igazán jól és gyorsan megvalósítani a blogmotort objektumorientáltan, MVC meg minden hasonló finomságot is figyelembe véve, pont akkor, amikor már halomban állnak a fordítani valók és az ellenőrizni való fordítások, amikor jövő hétig meg kell írnom egy amúgy egész érdekes beadandót, amire attól függetlenül egy napom legalább rá fog menni, amikor egyébként is egész hónapban semmi de semmi nem akar normálisan összejönni.

Úgyhogy most fogtam egy jó kis eszpresszót, aztán ha kihűl annyira, hogy ne égesse hólyagosra a nyelvem (számoljunk vissza harminctól, igen, köszönöm), nekiugrok és ledarálom a fordításokat, elkezdek forrásokat gyűjteni a beadandóhoz, aztán ha marad bárminemű energiára kicsit is emlékeztető dolog bennem, mire ezekkel végzek, akkor nekiülök tervezni a motort. Mert azt az elmúlt n+1 alkalom nagyon jól mutatja, hogy ha nem tervezem meg előre az utolsó ellenőrző regexig, akkor valami teljesen váratlan helyen be fog bukni az egész. Bár a hónap hangulatához az is illene.


Fonts and webdesign and all that stuff

Recently I’ve been working on making a valid, semantic and accessible HTML5+CSS3 page out of a bag I’d seen in a local supermarket). I’m using Chromium most of the time, because it updates often and has support for the most cutting-edge stuff as well.

The problem is, other browsers don’t. So even though it looks all nice and nifty on my screen, if i reboot and check it on Windows, the situation gets complicated. Luckily IE 9 supports everything i need for the design (more or less), except for the text-shadow property, but that’s just enough punishment for people using IE.

However, across browsers there’s this troubling thing with the included fonts, that they simply don’t switch to the japanese font for the japanese text, but instead use the system’s build-in (and in the context of this design, ugly) defaults. It’s especially annoying because i even define the Unicode range for the japanese font… I guess i should just do the same with Museo, the other font used. But it’s effort, because i’ve to look up exactly what character range is covered in it. I’m afraid it’s not even a simple range… But if not even that could make the browsers use the japanese font, i’ll be really lost for clues as for what hack to use… (I guess some fancy JavaScript solution with character range regexes could do the trick, but it’d be an ugly solution for sure.)


Hash auth

Until now i was planning the blog authorization (which would by the way be used only for the admin area, as no user registration will be left) to be something like the user (that is, myself, considering probably no one else would use the admin area) logs in, i give the user a cookie, everyone’s happy. Usual, isn’t it?

The thing is the content of the cookie. I was thinking that just hashing the login creditentials (that is, what’s stored in the database, eg the email address and the hash of the password) would suffice for authentication, but today a plurk about wireshark made me realize it wouldn’t. So the new method is to hash the ip address of the user in the cookie as well. This way, unless the attacker can somehow get behind the same ip as the original login, even if they had the contents of the cookie, they couldn’t impersonate the logged in user.

Or could they?


Félig kész

Félig elkészültem a vale subs kódjával – vagyis lefelé már működik, mindent betölt ahogy kell (elvileg). Ami maradt, az valami admin-felület nekem, hogy ne kelljen mindig phpMyAdminba mászni, ha publikálni akarok egy fordítást. Ez még bele fog telni egy időbe, de a látogatóknak ebből úgyse fog semmi feltűnni.