Oops, sorry for spreading mis-information - to be honest I'm a bit of a python noob!
Wow that regex stuff is pretty powerful - gonna look into it some more! Some great tips & things to be learned here, so thanks again for taking the time Justin, the members of this group are very lucky to have somebody like you around! On 18/05/2013, at 6:24, Justin Israel <[email protected]> wrote: > @Ben - I think you meant to suggest "my_uNkle_bobo" ? He was creating a > folder with spaces, but spaces should pretty much always be replaced with > underscore for portability > > General: > Normally you would move that version number to the root of the module: > > __version__ = '0.1.2' > > That way you can import your module and check the version, as opposed to > needing an instance of a single class (you may have more than one class > eventually) to check the same version number. > > --[ Separator support > Even though they are just fine on windows, spaces in general are just bad > practice because of the portability issues. > > Your clean_seperator() function is doing looping over each character and > building up a string using concatenation, which is a pretty slow process in > general. > > You can use a regular expression to replace whitespace with underscore: > http://docs.python.org/2/library/re.html > > import re > filename = 'a silly ! looking @windows - file.ext') > re.sub(r'[^\w_.]+', '_', filename) > # a_silly_looking_windows_file.ext > > That would replace anything that is not 0-9 , a-z, A-Z, underscore/period, > with an underscore. You could tweak the regex further to suit your needs. > > --[ Multiple file versioning > Trying to follow the list comprehensions, but it appears this method makes > quite a lot of assumptions about the file name format. Will just step through > it from top to bottom with my comments: > > backups = sorted( [ n for n in os.listdir( path ) if n.startswith( name ) > and self.seperator in n ] ) > > So as long as it starts with the substring and contains the designated > separator character, its a valid backup match? > > x = 'my_valid_backup_v02.mb' > y = 'my_valid_v03.mb' > x.startswith('my_valid') # True > y.startswith('my_valid') # True > > Again, here you may need a regex pattern to find existing files matching a > concrete versioning scheme like "<name>_<version>.<ext>" > Also just an optimization suggestion, but your call to get the backups first > gets a list from os.listdir(), then makes a list for the list comprehension, > then a 3rd list returned from sorted(). You could make use of a generator > here: > > aGenerator = (n for n in os.listdir( path ) if n.startswith( name ) and > self.seperator in n) > backups = sorted(aGenerator) > > Which can also be written simply as: > > backups = sorted(n for n in os.listdir( path ) if n.startswith( name ) > and self.seperator in n) > > While the listdir will still expand to a list immediately when the call > starts, the generator will yield a name each time the sorted() loops for a > new value, instead of passing it an entire temporary list at once. > > When checking if a list is empty, you should write: if not backups > instead of creating a new temporary list in memory to compare against. This > will also keep your code from breaking if you decide to switch the backup > object to be a set and forget to change your explicit comparisons. > > When parsing out the version, again a lot is assumed. You loop over each > component split by your separator, looking to see if it starts with your > version prefix. In your case the default version prefix is 'v' which is one > character long. But what if it were 'ver', or an empty string? You are > explicitly removing that first character no matter what the prefix is. > There are also assumptions of the amount of period characters being used, and > where you would expect to split to find the extensions. I have seen this > kind of file naming scheme before: > proj_scene_shot_descript.justin.v01.mb > If you use os.path.splitexit(path), that will split off the actual > extensions. Or you can be more generic and use aString.rsplit('.', 1), which > will split from the end and only split once. The difference is that the > latter will not give include the period. > > --[ Extension query > > The dict mapping types to extensions seems appropriate, being that you are > using it to actually look up a mapping from type => extension in your save() > method. If you were only using it to loop over extensions to try, as you do > in your clean_seperator(), then I would say a set() would be better than a > tuple. A set() is like a dictionary with only keys. They are unique and > unsorted, and are much faster when you want to test membership ala: "foo" in > mySet. > > --[ Notion of duplicity > > os.path.islink() only tells you if the path is a symbolic link. A hard link > is technically any normal file, as the act of making a hard link is just > creating another link to the same inode. You can check if a path has more > than one hard link: > os.stat(path).st_nlink > Normally that will be equal to 1, unless you have created hard links. > When you save the scene, Maya is replacing the file with a new file, which > points to a new inode, so the only remaining link to the old one is your > backup hard link. > > -- justin > > > > > On May 18, 2013, at 4:19 AM, Ben Muller wrote: > >> take all the spaces out….. most older applications of terminal/command >> prompt do not know how to resolve the space char, so instead you need to >> deliberately acknowledge vacant spaces >> >> I'm a terrible teacher, but as an example: 'my uNkle Bobo' - should be >> my/uNkle/Bobo' >> >> never ever have spaces in your filename with maya - - - EVER --- it's >> almost the equivalent of dividing by zero >> >> good Luck@ >> >> On 16/05/2013, at 7:29 AM, Jesse Capper <[email protected]> wrote: >> >>> Why is my unkle bobo broken? Windows works with mixed slashes and that >>> filepath should resolve fine. os.path.isdir(os.path.join( scenepath , 'my >>> uNkle Bobo' )) should be True (assuming the directory exists). >>> >>> You're getting mixed slashes because \ is the windows separator >>> (os.path.join uses os.sep). >>> >>> On Wednesday, May 15, 2013 9:06:03 AM UTC-7, Ævar Guðmundsson wrote: >>>> Thanks for the excellent replies! >>>> >>>> Further argument goes without saying I'd think, it's not only more >>>> efficient and code friendly to make use of the python standard path >>>> library as well as readability within the code is greatly enhanced when >>>> using os.path for path splicing and 'string'.join() for other joining >>>> things and while there are no personal griefs going on regarding the >>>> shutil module the process of copying a single file into a directory should >>>> be rather straightforward and effortless. >>>> However, I've updated my code accordingly and was about to try out shutil >>>> vs ctypes.windll.kernel32 when I noticed my destination path wasn't >>>> getting picked up... >>>> >>>> Explanation coming in this simplified example: >>>> [code] >>>> import os >>>> import maya.cmds >>>> >>>> workspace = maya.cmds.workspace( q=1,rd=1 ) >>>> directory = maya.cmds.workspace( 'scenes' , q=1 , fre=1 ) >>>> scenepath = os.path.join( workspace , 'my folder') >>>> >>>> print scenepath >>>> print os.path.join( scenepath , 'my uNkle Bobo' ) >>>> print '/'.join( [ scenepath , 'tends to join things' ] ) >>>> >>>> [output] >>>> C:/Users/aevar_000/Dropbox/Samansafn/Verkefni/Cube/my folder >>>> C:/Users/aevar_000/Dropbox/Samansafn/Verkefni/Cube/my folder\my uNkle Bobo >>>> C:/Users/aevar_000/Dropbox/Samansafn/Verkefni/Cube/my folder/tends to join >>>> things >>>> >>>> [resolution] >>>> My uNkle Bobo is broken, path constructed him using two different slash >>>> schemes. The / symbols are what Maya is returning and \ is coming from >>>> path. >>>> >>>> Any ideas? The platform module comes to mind but I've never used it that >>>> much so any info will be great, or perhaps I should just create a custom >>>> filerule and query that one with fre='custom' ? >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Python Programming for Autodesk Maya" group. >>> To unsubscribe from this group and stop receiving emails from it, send an >>> email to [email protected]. >>> To post to this group, send email to [email protected]. >>> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Python Programming for Autodesk Maya" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> For more options, visit https://groups.google.com/groups/opt_out. > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
