cosmogonies.net Blog of cosmogonies.net

search
29Apr/121

APyTA_08: The devil as a string.

APyTA_08: The devil as a string.

I may have had warned you about the danger of dealing with the name of the object instead of the object itself but...
Let's talk a little about strings, as you will have to often manipulate them (renaming and finding stuff mostly).
First rule : a string is.... a list of string (of one single letter) !

mySalutation="Hello_World"
print mySalutation, type(mySalutation) #Hello_World <type 'str'>
print mySalutation[6] #W
print type(mySalutation[6])#<type 'str'>

Unlike other languages, string is a basic class in Python and have a lot of useful methods.
Let's see examples of the very powerful renaming tools including in python which comes very handy:

mySalutationFriendly=mySalutation.replace("ello","i")+"_!"
print mySalutationFriendly #Hi_World_!
if mySalutation.startswith("Hello"): #Testing a start (you can also do a endswith)
  print mySalutation+"_"+str(42) #Hello_World_42
  print mySalutation+"_"+str(42).zfill(5) #Hello_World_00042

Please notice this replace methods does NOT change your variable content, so if you want to store it,
you will have to re-affect your variable with the result (And remember you can cascade the methods as in any classes)
Python is capable of polymorphism, but is STRONGLY typed. That mainly means for you that you can't concat numbers (int,float) and strings,
for example when printing a variable , so you have to explicitly convert your number as a string as follow: print("MyVarAsNumber="+str(MyVarAsNumber))

If you want to search if a substring is into your string, 'in' will do that perfectly, and split allow you to return a list of cut string, defining a separator.

if "_" in mySalutationFriendly:
  myList = mySalutationFriendly.split("_") #You can split on any character(s).
  print myList #['Hi', 'World', '!']

If you want to add special characters into your string, you will have to enter a specific code with backslash: \n or newline, \t for tabulation.
For including a backslash into a string, you have to escape it with another backslash: \\  and at last, for including a quote, you also have to escape it : \" .
But Python have anticipated that, and as he have three different string constructor: quote ', double-quote " and triple-double quote """, you can trick your string with that easily:
"""  Hello, currently my pseudo is "JohnDoe", but some of my friends call me "Mister '<tag>'  "  """

Finally, if you want more power to match a pattern into a string, you will need the 're' module, like that:

<pre>cameraName ="SHOT-Aa_C001_V01"
import re
print re.findall(r'\d+', cameraName) #['001', '01']
pattern = "SHOT-[A-Z][a-z]_C[0-9][0-9][0-9]_V[0-9][0-9]"
print re.match(pattern,"SHOT-aA_C001_V01")!=None   #False
print re.match(pattern,"SHOT_AA_C001_V01")!=None  #False
print re.match(pattern,"SHOT_AA_C01_V01")!=None  #False
print re.match(pattern,"SHOT_AA_C01_V0")!=None  #False
print re.match(pattern,cameraName,)!=None  #True

So, for pattern matching, use the re module and search a little bit more about REGEXP (Regular Expression). I will make a post on that module later anyway.
About special characters, be warn that string will handle ONLY ASCII letters.
So if you need to be able to manage special characters, like accentuation, you will need to use unicode (A next APyTA will deal with that too)

Also, dealing with path could be really painful.
AFAIK, you can write a path in three major styles:

<pre>MayaPath = "C:\\Program Files\\Autodesk\\Maya2012\\bin\\maya.exe"
MayaPath = r"C:\Program Files\Autodesk\Maya2012\bin\maya.exe" # r is for RAW, tels python to take your string "ASITIS"
MayaPath = "C:/Program Files/Autodesk/Maya2012/bin/maya.exe"

The last one is linux-friendly, do not care about escaping  backslash,
BUT I shamefully use the first one... because I mostly work on windows,
and because path are often computed from parts and pieces coming from your app, other tools, etc... and you have to be really sure they are formated the same to concat them...

Comments (1) Trackbacks (1)
  1. In addition to the last path part, You can concat path using a
    “os.path.join(RootPath,SubPath)” but I dislike that.
    In an old-fashioned way, I still need to know how are the slashes in my path to maintain consistency.


Leave a comment

profile
home