Just A Small Guideline

Feel free to browse the site. Don't be afraid to leave comments, they're supportive.
Please follow me on Twitter @ehldmnt and retweet my posts.

Thursday, July 18, 2013

[Python][Tidbits]Building A Python Module For "Personal" Use

My module has three different functions. Though, I feel that three functions is too many. It would seem that by using *kwargs, between the parenthesis I cannot create any other kind of parameter. *Sigh* I guess I'm going to have to either rebuild it, or edit the README. My goal is to minimize the code and maximize the performance. The construction of a Module that would be used "strictly for personal use" is not a bad thing here or there, but in excess, you'd pretty much spend more time writing modules, less time actually getting some things done. And that can be fun, but not when you're tried for time, or on a deadline.

You're better off googling around for code that someone else created, read the API, and then move on.

As a side note, I've recognized an issue in a scripting style that I've used just recently, the problems very easily being solved by reading over the Python 3 documentation, which I've only done about once or twice, but not as heavily as I have recently. Built-in String methods. Everything in Python is an object of a class, in this case: str(). A few good ones are:

.startswith():

This tests for a specific set of characters at the beginning of a message. It's case sensitive, so be careful. It returns true.

.endswith():


Tests for the end of a statement, this can be useful, though is restricted as far as case is concerned. Returns True.

.upper():


Makes all of the letters inside of the string uppercase.

.lower():



Makes all letters in the text lowercase.

Pro Tip:

When I first started out, I used to make massive, and ugly logic:

yesorno = input("Y\n ")
if yesorno == "Y" or yesorno == "y":
somecommand()
else:
 someothercommand()

Now I just:

yn = input("[Y\n]: ")
 if yn.lower() == "y":
somecommand()
else:
someothercommand()

It's just neater, easier to read, and far less code.

.capitalize():


This capitalizes the first letter in the first word. I used it here to capitalize a name. Good for possibly forms that would require name data.


.casefold():
.





Similar to .lower() but instead removes all case specifications in general.

They can also be compounded.


I hope these thoughts were useful. To read more about string methods, visit the Python Documentation.

No comments: