Often Python is referred to as flexible and easy to learn programming language, mostly this is because it lacks some of the features you normally don't need that much. One of those features is const objects: being able to protect a variable against future change. Some languages call this "final", others "const", here is the best way I could think of to do this in Python:
import contextlib
@contextlib.contextmanager
def constant(vars):
''' Protect the return values of (callable) vars from change
Use:
def f():
def constants():
return a,b
a=12
b=24
with constant(constants):
print 'Do something'
#If you would use b=2309 it will raise an exception after the with block.
'''
__protection_information = [(id(i), hash(i)) for i in vars()]
yield
for i, v in enumerate(vars()):
pi = __protection_information[i]
if not pi[0] == id(v):
raise Exception('id() of const variable %i changed' % i)
if not pi[1] == hash(v):
raise Exception('hash() of const variable %i changed' % i)I had some time to kill, so I pulled up my system monitor and had a look. One thing bugged me: Firefox was doing something while I was not doing something with Firefox. So I dove into the settings and documentation and this long post deals with all the things I could find.
One problem with https is that it is not possible to do mass virtual hosting, because the certificate is connected to the hostname, but the hostname is determined by the request. Chicken and egg problem there. In the end you can make a snake-oil certificate for one domain, but it won't work with other domains. If you decide to strip https support for those people who find a way to get there, here is a .httaccess file that will do that:
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule ^.* http://%{HTTP_HOST}%{REQUEST_URI} [redirect=permanent]
You may already know youtube-dl, a popular Python script which allows you to easily download a youtube video. Well, there is another program which mimics this behavior, but works for more then just youtube, and it's called cclive.
CClive started out as a perl program under the name of clive, but the C++ rewrite is simpler and has a smaller memory footprint. Cclive has the same usage as youtube-dl:
cclive <URL>I wanted to use this, so I decided to package it for Ubuntu, there was no package available yet, so this blog entry is to tell you about my new PPA entry for cclive. Debian unstable has a package for it already, so for debian you can find it here.
The 0.5.5 release supported sites are
People often say that Python is an easy language, but like other easy languages it is often harder to make sure you are working in a safe manner. I'm not talking about security, but bug-free.
A) Assert allot:
assert something == True #This must be true because...
assert isinstance(variable, int) #This algorithm only makes sense with intergersAfter being annoyed by the snapshot filenames of gitweb, I started looking around. A patch to fix the problem has already been submitted to the gitweb developers and if they see the need it will be part of a future release. Bored of waiting, I tried cgit.
cgit is currently at version 0.8.3 and still needs some testing and security checks before it gets to version 1.0 (according to their own README), but that is no reason not to try it out locally.
At the time of writing there are were no packages available, so I decided to create one and post in to my PPA. If you have apache set up and running and know a bit of what you are doing, get that package and let's take it for a spin.
The new Karmic is missing something that I used to use: gnome-power-cmd (formally gnome-power-cmd.sh). To fill this void, I had to dive into the internet to find what could replace it.
The idea behind gnome-power-cmd is that you could make suspend, hibernate and reboot calls from the commandline if you where logged in as normal user, not needing the root privileges. It was a userspace variant of the pm-utils (pm-suspend, etc.) which all need you to be root. The death of the command (which was simply a shell script) probably came with the HAL to DeviceKit transition. So, on Karmic it seems that neither HAL dbus messages, nor gnome-power-command calls will work. This is the closest thing I could find:
dbus-send --print-reply \
--system \
--dest=org.freedesktop.DeviceKit.Power \
/org/freedesktop/DeviceKit/Power \
org.freedesktop.DeviceKit.Power.SuspendThat should suspend your computer. You can substitute the Suspend with Hibernate if you want, although I haven't tried the latter as it doesn't work with my computer (on boot, it just ignores it and nothing is restored). A local replacement for gnome-power-cmd shown and downloadable below
I had this typo in my code, and it gives you the weirdest errors ever:
translation = range(20)
translation = [[i, str('Number %i' % i)] for i in range(20)]
print 'Translations set: ',[i[1] for i in translation]
#Split the list, so we only take the upper part
firstN = 5
print 'First %i translations' % firstN, [i[1] or i in translation[:firstN]]Jumping into a new language can be allot of fun, but your pervious experiences may hurt you more then they help. A good example of that is jumping into Python with C++ experience and trying to use default arguments. Simply enough you say, after doing some of
def fun(a = 0):
print a
a = 10
print a
fun()
fun()def fun(a={}):
print a
a['hello'] = 'good'
fun()
fun()To open up your ufw firewall for DAAP on the local network (ipv4 rfc 1918), you can issue the following:
sudo ufw allow proto tcp from 10.0.0.0/8 to 10.0.0.0/8 port 3689
sudo ufw allow proto tcp from 172.16.0.0/12 to 172.16.0.0/12 port 3689
sudo ufw allow proto tcp from 192.168.0.0/16 to 192.168.0.0/16 port 3689To delete them again, use the same but with "delete" just before "allow".