Every programmer should learn a new programming language once in a while. Be it for inspiration or just for fun. After some homework in C# some years ago and quite some Java at university (again), Python was my language of choice. This is the first article in a series I plan to write about what I like in Python, what is disturbing but works out and what is really evil. In this article I give a short overview on my motivation to dig into Python and start with a first topic: The missing braces.
This entry is part of my series Python. Good, bad, evil, where I discuss my experiences with Python from a PHP developers point of view. Find references to the successor episodes in the trackbacks of this post.
Learning a language just for the sake of doing so is cumbersome. My motivation for it was, that I needed a well established and responsible ID3 tagging library. Neither a PHP lib nor extension, nor any shell tool I found satisfied my needs, but Pythons mutagen did.
After having tagged my MP3 collection quite well using Musicbrainz Picard, I wanted to have reasonable genre tags for this, too. There is a plug-in for Picard, which tries to determine genres from Last.FM tags for a specific title by a specific artist. I liked the idea, but the plug-in has some serious drawbacks. So I finally decided to take the jump into Python and started with Pagger, a semi-automatic solution for genre tagging on the shell.
Beside the very nice mutagen library, Python is one of the most used scripting languages nowadays. As PHP does, Python allows you to code object oriented, procedural and functional. In this sense it is quite similar to my favorite language. However, Python has some fundamental differences in respect to syntax and semantics, which I found interesting to experience. In addition, learning about different approaches from outside ones own community is always a good idea.
In each article in this series, I will tackle another feature I found interesting while working with and reading about Python. I will show you some small examples to illustrate the discussed feature, discuss the pros and cons I see and conclude with an opinion if I would like to see such an approach in PHP or not. I'd love to read your opinion in the comments, so feel free to discuss your own and my views.
This should not be an article about Pagger, but once mentioned, I'd like to give a short overview on what it does: Pagger runs on the shell and receives a config file and a directory with MP3s as parameters. The config file determines the genres you want to be used, mappings between tags and genres, ignore patterns for tags and some more configuration.
Pagger now tries to determine suitable genres for an MP3 from Last.FM tags and from Freebase. If it recognizes all of these genres, it automatically assigns them without interaction. If there are unrecognized genres for a file or none at all, it offers an interactive shell to add new genres, new mappings, ignores regex and more. Learning iteratively this way, Pagger has the goal to assign tags more and more sensible automatically with each tagged file.
You can find the Pagger source code on github. Feedback and contributions are very welcome. But beware, this stuff is below alpha state by now.
In contrast to many other languages, Python does not use braces of any kind for syntactical code structure. Don't be too shocked, when comparing the following two code pieces, that perform equivalent operations in PHP and Python:
<?php
for ($i = 1; $i <= 10; $i++)
{
if ($i % 2 === 0)
{
echo "$i is even\n";
}
}
?>
In Python one could achieve the same thing like this:
for i in range(1, 11):
if i % 2 == 0:
print i, u" is even"
I know that there are more elegant ways to achieve this in both languages, but examples should be simple and stupid.
I'll start with the cons of such an approach: First, Python code looks simply ugly for programmers that are used to common languages like PHP, Java, C and many others. Braces are the most common approach for structuring source code at all. Therefore, many many coders out there are highly used to that way and are pretty irritated by Python code missing them. It takes quite a while to get used to it, which keeps you from efficient coding at first.
Second, problems occur easily here, when inconsistent indentation is used. Python only looks for the indentation in number of space characters. This might be blanks, but also tabs (and maybe other space chars?). You need to stick carefully to your style of indentation, otherwise your code will break, not only the optical appearance. However, this can also be considered a pro in terms of coding style, as you will see in the next section. And a sensible editor will fix the issues for you automatically.
Braces produce many blank lines in your code. While this might sound senseless at a first glance, this allows you to visually detect mating structures more easily. Python code looks generally more compact, if you do not introduce blank lines by intention. Having to use braces forces a developer to do so, which helps the readability of code.
No matter in which language you program, sticking to a common coding style throughout a project or maybe even a whole community provides only advantages. Digging into code which looks like it could be your own (from an optical perspective) makes you feel comfortable and therefore eases the process of analyzing the code. Enforcing a common coding style is therefore a goal in many programming communities. Python simply ensures that every code piece at least appears highly similar to every Python programmer.
Typing braces over and over again is cumbersome. Although most developers won't even notice any more, every brace costs a little bit of time. In addition, some keyboard layouts make typing braces hard: For example on a German keyboard, you need to type <Alt-Gr> + <7>
to achieve a {
. Removing the need to type braces therefore reduces time and non-productive effort while coding. Although both affect you only a very little.
Code without braces looks and feels strange to many programmers, but having the compiler enforce a certain coding style is cool. However, beside the fact that it won't happen, I also would not want PHP to loose its braces. What I would like to see is a common coding standard which can be enforced by PHP using an INI setting.
Naturally, this would be the coding standard I am using, just for clarity. ;)
I will continue this series whenever I find some time to blog and no hot topic is in the pipe. This won't happen before March 1st, since I'm on vacation for a week now. Read you in a bit! :)
If you liked this blog post or learned something, please consider using flattr to contribute back: .
You can skip braces in PHP example too, because blocks consist only of one command. So this is not very good example (-;
Link to commentBraces save you, if a file is kind of broken. If python file is broken you have no chance to see where indentation goes wrong (very bad)
Link to commentMay be you'll write also about module organization. In php you have usually one (may be more) classes per file. But in python you have variables, constants, functions in one file! That is really cumbersome. You have often a python class and inside of class some other definitions (property) or whatever. IMO very chaotic. They should be at least sort functions and classes in module...
Link to comment
@php6
Python has had module support since forever. PHP just started using namespaces. See the Python documentation or Google for a tutorial if you don't believe me ;)
And: of course you have fields, functions etc. inside your PHP classes as well. That's how you do OOP!
When I first looked into Python, I really welcomed the no braces approach - I think it makes code much more easily readable. And given the legacy codebases I've been subjected to recently, it would be a true blessing if PHP scripts would simply break if someone got the indentation wrong (*shudder*).
Also, I find when reading books on software development, I can read the Python examples more easily, because it is much clearer which code belongs to which block even when the example spans over more than one page.
Regarding detecting mating structures more easily when using braces, I have to disagree as well... when I was wearing braces as a teenager, I feld that this highly impaired my ability to take part in any kind of mating structures, since braces are mostly not considered very attractive. Imagine how mysterious and interesting I would've been if I actually had a Python instead.... uhm, am I still on-topic here? ;-)
@trond
You've misunderstood me. In PHP you just use classes. In python you don't know if you should/could classes or function inside module. What I mean, that after php it looks like cumbersome, because it is mix of functions and classes in module. In well written php you never see such organization of files. There is strict separation between functions and classes. Usually, you have some script.php which instantiate classes, but you never see functions definitions inside of some Foo.class.php
module.py
def some_function():
foo = SomeClass
foo.doSomething()
class SomeClass:
def doSomething(self):
pass
http://code.google.com/p/mock/source/browse/trunk/mock.py
as an example. There are more of course.
IMO python is more strict (=stable) language as php, because of better type ckeking etc. etc. My point is, if you switch from php you see many strange things...
On the other side, there are no really private, protected members of classes, no comfortable (elegant?) type checking of passed classes
@php6
I am afraid you are mistaking "can" for "must" in Python's "code organization" (and PHP's for that sake).
Your examples are just examples of how you MAY choose to do it. You don't /have/ to organize your Python code in such a way. And: you MAY use the same approach in PHP, if you really want to. Try it for yourself (PHP):
function doSomething() {
echo 'Inside doSomething() and ' . anotherOne();
}
class MyClass {
public function doIt() {
doSomething();
}
}
$x = new MyClass;
$x->doIt(); // prints "Inside doSomething() and I even got to anotherOne()!".
function anotherOne() {
return 'I even got to anotherOne()!';
}
Perfectly valid PHP5.
You can organize your PHP and/or Python code this way. Generally, I wouldn't recommend it, but you can. Nothing special about Python there. So there is no "flaw" in Python's code organization capabilities here.
I bet there are tons of horrendous PHP scripts out there, if you want to look into bad PHP code organization.
(I'm more of a PHPer than Python'er myself, and have seen my share of crappy PHP code).
Of course, you'll observe differences when moving from one language to another. If you only know Lisp or Prolog you might feel that there are strange things going on in PHP as well. Or you may not.
In any case, I believe we should listen to Uncle Bob's advice:
"Languages are tools not religions. Superior attitudes taken by language zealots ring hollow before the craftsman's toolbox."
http://twitter.com/unclebobmartin/statuses/9180866711
well see here
<?php
for ($i = 1; $i <= 10; $i++)
print ($i % 2 === 0) ? "$i is even\n" : null;
?>
as you can see php can be "no-brace". And this approve that your equivalent is wrong.
From my view point, here code with brace look more logical for third party.. Just image if you have multiple logical blocks without brace.
python is more clear,simply,unified than php.
PHP is a tool.
Python is a language.
Tobias, thanks for offering to write a series, I'd like to offer my (vocal) support and tell you that for me, your series will really hit the spot.
I am someone who wants to learn Python but the reasons to do so need to become more concrete because I simply have so much work to do.
I have to know WHY I should learn it, I've asked this question time and again all over the web and the answers seem to provide an all too fleeting glimpse - but never the solid examples which cause me to go - crikey, thats something I could be doing in Python, if only I could programme in it.
In this post for example you explain not only the differences we will experience with the loss of curly braces, but that you get access to the Pagger library, which is cool.
Maybe further on in this series you could explore and explain how for a PHPer Python is better suited to some tasks than for others - and continue to give examples of what you mean.
Hope it goes well, and you have at least one avid follower.
Go Tobias.
I am also looking forward to your articles in this series - learning Python is something I have been planning on doing for some time to inject some freshness into my PHP world.
Link to comment
Ich bin auch mal auf den Rest gespannt, wobei ich zur Diskussion zum Coding-Style nur sagen kann, dass man sich auf eins einigen sollte, und die dann enforcen sollte, ob per commit Hooks in einem Repository oder was anderem ist dann egal.
Zum Thema "wo schreibt's sich am Schönsten" möchte ich noch Ruby erwähnen ;-) :
(1..11).each { |i| puts i.to_s + " is even" if i.even? }
/me räumt seinen möglichen Flamewar wieder ein und verzieht sich ^_^
(Oh, und ich sehe gerade ich hab's mal wieder geschafft die Bounds falsch zu lesen, wenn man im Beispiel oben die 11 durch eine 10 ersetzt macht es auch genau das gleiche wie deine Beispiele…)
Link to comment
锘?b>Red is the shade that we can't appear down on, particularly the red sole footwear of Christian Louboutin.</b>
Given that the web has become our companions in our daliy life, browsing on-line has previously acknowledged by a huge greater part of folks.The web is property to some of the best discount christian louboutin 2011, the place you can spend significantly less money to have your aspiration 1.If you acquire a lot more, you will locate that world wide web can save you a lot of income that you by no means knew you could conserve.Christian louboutin 2011 is confident to be stacked for you to purchase.脗聽What conspired and transpired between these two folks nowadays will be the specifics to ponder.Now, it definitely is appealing to seem over and above the argument to understand the various segment of conflicting activities.脗聽Discussion is outlined as, consideration of an situation in an open up and typically informal debate.Alternatively, continue to be amazing and compose and politely say that you simply benefit their arguments or debate but beg to disagree.Abroad christian louboutin footwear boutiques are situated doing work in london.Marni Jewelry Watermelon provides the purpose of performing exercising the interior temps, so if you get melon make your best energy, you can envision the consequence.then procured the confront, created photocopies, as effectively as block the quite a few confront portions to provide given that behaviours.脗聽The various locations are typically <a href=http://www.christianlouboutintsarpumps.com/christian-louboutin-hot-deals-c-74>Christian Louboutin Hot</a> lower on african american outfits which contains african american fasten, black color clasp not to refer to brown gloves, that would emphasis on this clothes.脗聽It might be a excellent reprise involving when Shiny as properly as collaborator Cindy Sullivan originated by utilizing Paddington Inn Cafe several a long time in the previous.Which will which signifies that, you can effortlessly knowing the particular magnificence and deluxe for secure wrist watches and no investing to boot noticeably bucks.Droped straight necessary a minute bulb beyond fairly textbox and dangle up it once again shared.脗聽a small time in the past,Gucci Purses Sale the actual gorgeous manufacturer identify prada components discounted protected in the unpleasant getting utilized environment, as time move, misbehavior alongside with bodily abuse linked with overtime on top rated of that Staff arguments Saturday 15 Deck palms dispatch family members and also Social Security Business office concerning Shenzhen Neighborhood is certainly the legitimate reaction: gucci storage compartments refugees greatest investigating.脗聽International System pertaining to Cellular or portable is a 2nd generation communicating customary created to offer you method options in addition to bandwith with digital modulation.They are merely way far too useable, handy.The resort capabilities all around 10 500 sq ft associated with adaptable sort assembly area and will deal with enormous sets to as significantly as 800.
<b>The distinct areas are normally lower on african american outfits </b>
|
Link to comment
Christian Schools Clean Up Cheerleading
Cheerleading (search) has strayed from the 2-4-6-8 rhymes of yesteryear, and that can leave parochial school cheerleading squads wondering how to craft routines that fit their values without looking downright retro.
One answer is Christian cheerleading camp. A growing number of Christian schools, put off by the sometimes-seductive dances and cheers taught at secular camps, are opting instead for faith-based camps and competitions -- places where Bible study meets basket tosses and the music doesn't have to be bleeped out.
Jaime Fulton, cheerleading coach at Western Christian High School in Covina, Calif., remembers going to secular cheerleading camps when she was in high school. When her Christian school's squad got home, they'd have to rewrite many routines -- putting them to music that didn't fit and taking out some of the wilder gyrations.
When she heard about the Fellowship of Christian Cheerleaders (search) -- a Lawrenceville company that mixes religious messages with cheerleading -- Fulton signed up her 20-girl squad and found it a perfect fit.
"It's very different," she said. "I would never go back to a secular camp. What we're trying to teach our girls goes against all the media, all the sexual stuff and bad sportsmanship."
At FCC camps, Fulton said, cheerleaders learn they don't have to sacrifice modesty to have cool routines: "It's not dorky. It's not '80s cheerleading. FCC now works with 15,000 cheerleaders a year in faith-based camps and competitions, with a staff of 100 coaches. CCA teaches 7,000 cheerleaders a year and recently built a 27,000-square-foot gym.
"We felt that Christian schools needed somewhere to go that's just for them," said Rose Clevenger, founder and president of CCA. At secular camps, "they can feel uncomfortable with the dress code, or maybe they have inappropriate music. Typically cheerleaders look like sex symbols and don't dress appropriately."
The camps work just like secular ones, but with devotional time added in mornings and nights. Most of the instructors are college cheerleaders who went to Christian schools when they were younger, and they're encouraged to talk about their faith. They tell campers that cheering is a God-given talent that can spread Christian lessons.
"We think it can even be an act of worship," said John Blake, FCC's event coordinator. "Being excellent at what you do in any facet of life, that can be a testimony about your faith."
The wholesome approach isn't just to soothe parents. Cheerleaders from Christian schools say they've felt left out at regular cheerleading camps, either because their skirts are too long or their coaches veto the music. At Christian camps, they all fit in.
"There's not the pressure," said Tracy Handey, a 15-year-old cheerleader at Humble Christian School in Humble, Texas. Handey's squad went to a CCA camp, where no one snickered at their skirts <a href=http://authenticchristianlouboutinweddingshoes.webs.com>black christian louboutins uk</a> -- which fall to 4 inches above the knee. "I like our uniforms because they <a href=http://authenticchristianlouboutinweddingshoes.webs.com>authentic mens christian louboutin shoes</a> don't show everything."
In addition to cleaner music and dancing, there's a stronger focus on good sportsmanship at Christian camps, coaches said. Handey's coach, Vicki Howell, said that the growth of competitive cheerleading has led to more taunting and off-color cheers.
When her squad placed eighth at a recent competition, but won CCA's Spirit of Competition Award for good sportsmanship, she remembers telling her girls, "That's the only trophy you'll take into heaven with you."
Fields with bold names are mandatory.
Python. Good, bad, evil -1- Missing brace | schlitt.info
Every programmer should learn a new programming language once in a while. Be it for inspiration or just for fun. After some homework in C# some years ago and quite some Java at university (again), Python was my language of choice. This is the first article in a series I plan to write about what I like in Python, what is disturbing but works out and what is really evil. In this article I give a short overview on my motivation to dig into Python and start with a first topic: The missing braces.
Python. Good, bad, evil -2-: Native sets
One thing I really liked in Python is the data structure set, which is built into the language core. A set in Python is defined as known from mathematics and beside mathematical use cases, it is helpful in everyday programming live. In this episode of my series on Python, I give you a short insight into how sets work in there, how you can achieve similar results in PHP and why I like the Python approach.
Python. Good, bad, evil -3-: Flow control exceptions
My grandma is a wise lady. She told me many useful mantras which I should repeat every night before going to bed. One of these is Never use Exceptions
for flow control. And she is so right with this. It is even so true, that every OOP newbie should get this mantra tattooed on his hand. During my experiments with Python I sadly found it violated in a central place: When it comes to implementing an iterator.