Bestand wählen

Writing Awesome Command-Line Programs in Python

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Erkannte Entitäten
so our next speaker is not Smith is a developer and trying to find you and in Edinburgh and apart from that not also runs Edinburgh Python user groups and his talk is writing command-line programs in by thanks very much for coming to my talk and going spend the next 40 minutes or so
talking here about the things you can do to take your common
programs the next level and some of the molecules there in the standard library and revoke woman that to have you do that so just quickly talk about my favorite subject to me uh 1 influx miss I would for fungal and we're hiring remember looking for smart people so if you're interested come talk to me after uh and I'm pretty much cool duty on most social networks for reasons that of as interesting as the sound so that I would normally put the word also uh in a talk titled but this talk is fakie based on a book that I read about this time last year called global some common applications and Ruby so I was stuck building a command-line application in ruby
and I'm not really develop so I kind of look around I came
across this but it really is an excellent so if stuck in the same situation I highly recommend that but really became apparent to me while I was reading it was that a lot of the libraries that are out there in really and there are similar libraries in Python Python just as good if not better price in this kind of thing and that's really what led to this talk
so let's just go quickly about why you might want to write command-line
programs because I think this is the only talk on the topic of this conference so it seems like kind of minor concern and yeah I'd be surprised if there are any developers at this conference he don't write command-line programs regularly to simplify that your family we develop like weight a call the web application so I would say that the reason to write a command-line application is that In many cases a command-line application is the best application which job we have a habit of thinking of them as inferior to give applications in various
web applications and I would argue that's not necessarily the case there are differences in applications that benefit from windows and buttons and menus uh but as developers we will know that we can automate processes programs run by programs if we have something like bash or even other scripting languages the other programming languages we can glue applications together really powerful way and besides it's much easier to write command-line application to the full web applications so why would we want to spend any time writing a good command line application why not
just shock a main function in R script and be done with it and that's because this I say you want to do you want to think about this do this properly because scripts on never throw away they never get thrown away you write anything you can run once run them again in a week's time then you need to read the source
code to work out whether it really did what you want money make some modifications so it's if you start from the beginning with the idea that this is a reusable applications and when somebody comes up to you and says hey you collect call script that there's something I need then it's already documented so they can understand the output they don't need to actually modify the file to to run it so let's just quickly about
what makes up the command line application so I suspect most people just kind of fell into using and then writing command-line applications without really thinking about what makes 1 uh Marconi what makes a good easy-to-use reusable command-line application so this is your program it's just a program it's just of but the functions may be some classes this is the thing that actually does the useful stuff without interface it's not any you select starts taking command-line interface so that so the 1st thing you probably think of all the command-line arguments the flags things that begin with minus a minus minus data that you passed on the command line to run the application server and I've been talking to people about the topic of my talk and we can have been getting this look as if to say you're doing a talk on Aug the 40 minute talk a lot about and it a lot more to it than that that we've got a standard in standard outstanding and and it kind of looks like finished but we haven't finished uh we've got signals so I had gone have raised handsome actually cost signal In a command-line application a while the way more than I expected OK number go into great detail signals when going kind of skim over the course of far too many slides and and never got configurations so configuration is not specific to command line applications of it you have theory applications as well but I think it's more important than theory applications you tend not to the user tends not to see the configuration it tends to be hidden behind a preferences payments of desire that uh whereas with a command-line application if you providing configuration you use is probably gonna have to write that they're going to have to know where to find that we can have to know how to modify this so the decisions you make a more important and then finally exit code is a number that's returned and the program's finished say whether whether it eclipsed by still other encountered now so start with command-line there's a bunch of options and there's 3 in the standard library and you find that most people who aren't experienced Python developers pick get up uh partly because is not alphabetically beginning anyone a plus is beginning it's so is the first one they see and it probably written some see um believed in cancers of essentially the same interface in other languages as they just can't go up so that's what I'm looking for the get-ups horrible it's really horrible program and that's not really polyphonic uh it does involve the your audience very well and others about up to pass a lot because it's really been superseded by us uh of course of all the same functionality as far as I'm aware plus some extra functionalities undertook about us briefly and in the 3rd party domain we have few options as well and docket is interesting because it takes the opposite approach to anything else that have used and docket to actually write your help text so the context you expect to be printed out when you run running your script to minus myself docket will pass that work out what arguments York your program should expect in and it builds an object that will pass the command line arguments that's kind of clever but having used it it's kind of fit lead to to get syntax right when you're writing the help and I found it just I should take a lot longer and it wasn't really here in the end but I quite like the idea was that this cross-platform so it's been implemented in more than 1 language so if you if you for some reason writing a script that is implemented multiple languages that may well be a good option for you and Clint is much more than a command-line parts of Flint is reported by Kenneth of rights uh and it's a whole framework for building online applications are highly recommended because it covers the whole cross-section of stuff and we're going cover in this talk and I admit that the 1 thing I don't like about it particularly is the command line pasta and but but it's it's a good option purity using different than you probably can want to use a common policy that comes with it then this click on this is great that the recently was released by common run after a couple months ago I haven't really used it a looks pretty powerful almonds and these kinds of libraries and I believe he solved a lot glocalization encoding issues so if you're having a can be running and a lot of different locales then click probably quite good option for you and then finally we have come pagoda which I don't think is a hugely popular library but I like the approach of 2 and I think it's it's simpler for certain things than across I'm going to quickly cover that with 1 slide after after talking about apart so I'm
sure used are passed in this room so and looked ominous and but you instantiate an odd argument passed and any tell about all the arguments you expect some of Moby compulsory some will be optional and then when you finish doing that actually called pass talks will go talk to a set of the extract the arguments work out which flags corresponds which values and it had you back a namespace object which contains the extracted values if it fails to somebody's busting invalid value that it will it'll essentially
it'll print out the help text and finish with target of 1 and so here we got no option optional variable code open practical name and that's what and Alex question mark means uh and so if it's not provided on just replacing
with would well this is just a silly Hello world and scripts and prints hello and the value of name and that's what happens when you run it so because it's optional we can run it without making this world to be running with the Douglas and printed of very skipped heads so we get the same output as we would if we just extracted that single variable from uh from uh the uh so you you might say why do we bother it's it turns out that that's that was 1 line more often the case that the just extracted from the 1st lesson of of the and but you get this when you minus myself so although we didn't specify minus myself and we didn't actually provide any help text any it anywhere and I wouldn't say what name what's the instance being we can give the user a picture of how they're on the application I think that's really powerful by now no longer write any scripts that use of fossil something similar uh simply because when somebody comes to up to me says that script you gave me how how exactly do I run got documentation so much you can say run it with minus minus help will will tell you what you should be doing and then come back that's the first one that is more complex example just covering some more of the details of our past a random and here with the first one is we've decided that should be a boolean value there's no value associated with the Bose religious on or off and so it's a store true storage true if it's available falls it's not available um and in the 2nd argument number we've got this type parameter when passing into the interest just the standard Python In type but you remember hopefully in is also a function that takes a string and returns an integer value and that's actually or you can you can pass in any function like that so that argument onto the type parameter so anything that any function that takes a string returns the actual type you want you can pass and so the user then when you namespace watch container types that you're after rather than you having to constantly converts things and it's nice to be usable so I had some our personal so uh supports subcommands so if you're writing a
much more complicated application that the the traditional unix single use uh specification then and if you try something like it we've got we've got some commands I add full-fledged removed um 6 stay you can do it tends to be very verbose it works very well and it's kind worth doing you have to be quite careful about how you structure out all the passing for the different demands so you don't just end up with 1 huge block of code in some ways it's configuration was code and so this is where comp goes actually quite good compared
takes a much more lightweight approach and in some ways it's not so powerful but iteration this kind of application you just want to get it done quickly uh complicated can be a really good options so I have no idea was especially pronounced on the go by the way I don't really know what that means so here you instantiate an application that's essentially all past and that provides you the decorator and then you just use that decorated for any function you want to be an entry point into your application so in this case we give an application greedy subcommand which takes an optional parameter to so process itself the function uh and the same with on creatures so I added another set and another set of of uh no good we can call this this program with concrete and we can again we can choose to call it the premature not if you don't provide a default value for the parameters then it becomes it's a compulsory parameter and you have to provide the running the angry command
and when run it looks like this and so is not quite a nice it doesn't have positional parameters frozen where uh but it's really really likely it's really easy to write it doesn't take up a lot of a lot of space in your case and security using third-party libraries accomplished for options
that was command line passing cricket will open to and the store input and output so I O is often an afterthought it's just kind of using our them to Princeton since script and but good I could really transform a command-line application from something that is just a hockey stick and if it doesn't take a huge amount of work it just takes a little bit thought and and possibly some libraries that are out there that that can really constant power so just before
we move on it's worth talking about standard that standard error cause I suspect most people don't really think about them term standard of is for the output of your program it's so if you think of your program is being applied and it takes some input spits out some output
that's kind of the data that you're producing you might especially valid to have a script that doesn't produce outputs especially rods have a script that doesn't take input but if your program produces data in some form of standard out this worship standard error is probably the worst it could be given as a stream size standard error is not an error string you can use it for errors the standard error is really for updates on how the programs running it's that it's it's kind of information on what's going on it's it's you can assume that the user is probably going to see standard error preserve the piping the output of the program to another program or file that they're not going to see that so so that they get feedback on how the programs running so the way I think about it is the standard out can be piped but to follow another program with standard error can be locked to that that that that the least because to the screen or you can say it to look for that's really what it's for so let's talk about looking looking driven the right way it looks to standard standard error by default because it's ultimate information think uh looking to really powerful library I wish I had time to find a good threat but um this isn't it you know I using command line programs quite a bit and again it's not a huge amount of code and makes a big difference in the rest of your program in terms of how making it easy to use to provide information on the OP information to users and so based called basic conflict to just set up looking um I since it's a warning by default so anything that's info debug and would be the um spat out the user uh the reason I do because program should be relatively quiet by default and and the you can provide a flag to allow the user to allow the program to be more verbose so that's what we've done slight further down just checked if they want to say 1 more information I set at the 2 levels down to the but you could consider that the info all depends on kind of how how you like she's looking levels it's up to you and then they showed that the Gospel and suggest how how you don't use of 3 of program for the nice thing here is that looking just takes care of how much information is is sparse the user after that it it knows the level that should be sparse out so it doesn't invade your actual code you can use looking as you would in any other kind of program and and it would just do the right thing after after running to so another thing that's worth thinking about the often don't think about is are you talking to a user and so I think we often assumed when writing program that actually standardized and go to consult and input and this problem can be fed to us from followed analysts which is slightly slightly weird that we have that but that's a different approach to the 2 extremes and it's really easy to do in Python um the main uh file types the standard inside that standard error all support is that if you are as of module and that just tells you that that stream is attached to some someplace someone and so the thing you want to think about the results of that it is so just taking a picture
it's so this is the result of running the program that if you run on its own it just says not none of its pipes to anywhere just coming to the screen and if he pointed to a to another program in this case cats then it says the output is capital of standard standard error and finally just pointing in but now not the services is just testing the model works but thing you really want to think about with this is you can change the format of your output based on whether it's going to follow another program so if your information is being printed so this is the screen you may concern is that the user can read it so you can think about formatting stuff aligned columns you can think of color uh and you can think about the type of information they want to see how they want to read it this you pointing to a file or another program you want something easy possible so you could switch output adjacency field you're it's something that's easy for another program to pass becomes data rather than information
so I just mentioned color and color is also surprisingly easy
and we're really guys do this all the time said and we didn't see
so much color output from Python programs and you can just use and Stanley and standard ANSI state characters uh but identifies I recommend you use a third-party library Colorado so color armor provide some constants but for changing the streams and all that really does it spits out character to the system standard out uh and so here and I'm set and printing out rats and I print out some
text and standard that will consider continue to be printing red text and so I sent the reset token and so the next line actually have a background color of green then get rid text with being background
and that's kind what this looks like a you run and you can see how this would make a huge difference to the program if you got errors coming at red warnings coming and you government that's coming and white it is means that as your reams of information stream
past you can kind of see that that something happened that's actually interesting rather than just information and that makes a big difference long running programs and makes big difference with programs spits out a lot of information the problem with this is that is it would that script if I take the output to a file and the ads that it keeps the tokens and you don't really want that you want to store plain text and saving in a text file and and the way you do this and this isn't something that's really promoted documentation is you call cholera model in it and you pass in strip equals true and that just means is in this case which is saying if it's going to the Father and another program and strip out all the cactus Colorado 1 of the new features is that wraps standard that standard error of that when you initialize it and it means that if you print out those kinds the color codes that are in sports and units not windows and if you run Windows is the progress being run on Windows it will automatically convert into the Windows equivalent so just with kind of a single line of code um you essentially majored the colorization at least of your application cross-platform which can be kind of the user credentials something that's supported the standard Python library of the batteries included um and I just to just to quickly cover it's got t functions get past get past prints out password to the screen and then allows users to type in a password when they hit return and it returns as a string and it doesn't actually print the password to the screen and yet get user is quite different get user is not an interactive function that's that's slightly confusing get election talk to the operating system and trying to take the username of the users currently locked in so in this case I was locked in his market just pick that up and again it can be highly useful if your scripts is communicating with a database you don't have your password stored in the script is fundamentally insecure you could end up there and committing activation control something so if you want to be a professional applications should be asking the user will the password is uh perhaps you can
store any configuration file and progress is number 1 so if you got a long running task but can't count the number of times from a program doesn't doesn't spits every much information and you kind of think has it died should cancel it is against the should again is a network problem is it just a temporary thing will it will carry on working so you progress is a really nice easy way of just saying you know with we're in the middle of something and it's still going progress you can get an idea of how long it's going to take complete as well um so there's library and progress but which has been around for a while and I think it's really really needs love and has been forked such received some of which is different places that this is progress bar to keep the installed progress and you get a logical progress bar confusingly uh and then you can instantiate this and it you can use in a couple ways in this way wrapping an iterator eligible and uh just each time through it knows that it's an extra 18 through the process and it just print out the this progress the line with a percentage in front of it uh and you can see much more complicated things if you want to um so here is has this idea of a list of widgets that make up your progress bar line this is a little bit messy uh but it demonstrates if you think so you can have strings and you can have political widgets which a kind of updated each time so in this case can print out a string loading at the start this year the common bottom uh and it's a print out the percentage of is true that's what spaces imprint of progress bar and then the last 2 kind interesting CETA just keeps a running a calculation of when when the process can finish so that's that's kind of that would be something get paid develop itself a file-transfer speed assumes that every time you updating you're actually updating with the number of bytes that you've read and so it keeps a running tally of the number of bytes per 2nd which again is kind of years of professional level of updates if you're writing uh program streaming stuff from 1 place to another and that's that's quite nice information to the user and so what I showed you want to think about adding a flag to specify up so I said you can automatically decide and you probably should but I will always let the user rover right that because they might not want that to somebody to a fall they might want the output that they used to on the screen for some reason that the same with reversed in quietness at flag and to allow them to choose how much information is the um and also be responsive to tell you user that things still going on than just leave with a blinking cursor black screen of the the bad so let's talk about configurations and we have a bunch of options the configuration and we have way more just out of the box in the standard library than I that I thought that was little until I went looking like kind sort false adjacent to space of about is uh but it's a yeah we've there's a whole bunch of stuff we get out of the box so they can any faster use environment variables uh we can use Jason of books CST that there's some
rudimentary passes in Python not fantastic performance but that finds the conflict but I don't think anybody wants to write XML configurations um weave Apple list fill in all platforms which also I don't recommend um although obviously we could write our own policy using regular expressions of something horrible to and again just standing there uh surplus would go yaml legible working government with sensible recently and young turns out to be quite nice format to write and uh it's it's it's got all the same data types station but it's actually is kind nice to write and encourage other properties which you probably anyone's considered here interacting with Java application
about any files and the kind of the kind exciting and they have a lot of its Unix developers and for the thinking is that Windows things but I think that that he she powerful and I think people kind of underestimate the power of and possibly just just using the right way so I want to cover as the config pass a module has a few different config parts of objects in there I recommend you config pastor because that way you're applications to this and security holes in some of the others and they're there for historic reasons more than anything else as far as I can tell you and the way that users you instantiate your config posture which point you will have an an empty configuration objects and then you call read and read doesn't take a single file name it takes a list of file names and which is kind of counterintuitive into the understand really what it's designed for and what it does is it goes through that list of paths and if the file that that of points to is that we did and it will merge with other data in there with with with with others in your confocal jets and will go on to the next file and if that's the then it will read an image and if it's not there will just get it doesn't there are no errors if it doesn't follow find any of the files expects to begin a list of effectively optional files possible optional exist um so the way they use this and get by just talk about these parts of the first one is a defaults files that 1 assumes is provided with the script and and this is expressed in the same directory it could be in uses there should be a could be in slash sexual something that is sent you the full configuration uh and then on top of that we've got the you per user configuration so you would pick him tool that any from the home directory um and then on top of that we got config any which is our current working directory haven't specified announced path for that's that's just the way we are sets that year that your project configuration and these might look
like this starting in the bottom to the first one is slated is default study any and that this
user normally when you run this script whatever it does it's going to be talking to local host port 80 and and then it's a lot of talk about your um and then what it does is alleged tools to if it's not there at those of us who doesn't like those doesn't complain but if it is the loads and overwrites support so this is like inheritance with objects um so now we have a local host and port 5 thousand um and then uh then late in the project configuration for this project we're pushing everything to www . ninja star the period uh and lovers new domain names uh and so now we've got that placed got couple 5 thousand but we still have this Euro uh value is
generated with placeholders built up from western port and so if we print this out we got the hostess at the port said you notice that also we call gets into a dozen basic costing forces from passing from 1 and it's not a huge benefit it's kind of um and then we got euro which is generated value you notice that it's generated on demand so it's it's got the values that we inserted in later files and in Duroc start wasn't around in the in the file that Uriel was defined it but it still calls and anyway and that may not seem like a huge benefit and ability as well but what it means is that it the user wants to use an HTTP yes you're well they don't need to change any code that it's going to that defaults following changes they can just set your in that had their personal comfort the project config file and use that and again they can use these patterns they can use most important and so does need to be that didn't start from scratch so this is kind of powerful that's that's
conflict and so I'm I'm going to kind given 3 signals but that talking really fast
action in this paper we can follow me um signals so that is
not my spirit and that you can set up a callback essentially so when the signals received from the operating system the call your function and what the 1st line is doing is it's overriding the default behavior so when you when you function returns this seek info which is a non standard for like I can't actually remember how you pass it to your program but it can become useful if you programs not spitting out the output and users still wants to get some progress of DD does this you can pass in a 2nd phase and using a control letter sequence and and then it will just print out has progressed well which is which is kind of but it's the truth is probably most of somebody that very often the way we usually interested in which is
KeyboardInterrupt so when his submits Ctrl-C to finish your program um and that is actually caught by Python anyway so that that is not doesn't doesn't by default just quit your program raises a keyboard interrupt and this is why in all my command line programs are everything with the keyboard interrupt because the default behavior is to print stack-trace exit with an error which which isn't really what happens you generally want you want to handle and and not really do anything and I'm not really sure whether the correct behavior would be to make sure you exit code is 0 and this happens because is not so
and just gonna run very quickly through code structuring packaging um because I don't think I had enough times people and so all it's a different uh and a lot of of stuff I work on actually single-file strips and this is not a lot you get out a set of of the way to make instable uh you don't need to do you much uh and and often skills they just end up sent by e-mail which is not break his injuries versioning usability itself takes takes on that uh but there's nothing magic
about command line scripts I recommend you setuptools rather than distutils because the handles dependencies and installing independencies might have and also includes some some extra features specifically for command line right in content programs and so you we got set of but the way we've written all of example on next slide and we go our actual script which is my tool and that should be tiny should be really really small so we should be writing more likely to be reusable we may be writing a great force command-line application but the 1 that we might want state of functionality stick a web interface I want to take a stick carry on top of that when we might want all 3 at the same time the depending on how the user starts the applications so really you want all your functionality in the library as much as you can so my tools should really just call out to something inside my to live and you can put that in a in a couple places I added the double underscore main filed this morning so I have to know that the people have already come across it I think it was added with termed by 2 . 7 of the 3 and in that you never you spoke to run packages you could run modules of fuel your like previous distributed as a single file you could run that you have no way to run modules now if you if you run my to live with Python minus said and my to live then and they will look for the double on the school may file and execute that today is not a new special could important really wanted to this not designed to be ever imported it so he designed to run is the entry point into program so I think be tempted to put my command line the interface in that file where the sum of the uh and which is going modify files the fraction functionality and as such great recently my to look the way he tells the that's that's a reasonable way of structuring a relatively small and scripts and in the sense of the y if maybe if anybody hasn't written 1 of these before they they look a little bit overwhelming the huge amounts of of just data and
it's a function call as well it's not a data structure uh and so they just a bit scary and it turns out when you write and they're not that scary and I recommend kind of but is a bit of a cargo cult but I recommend finding some the command line script that that you've installed with it and when you respect the developer some where the script has been well developed I recommend recommend you going to look at as as a whole host of options depending on how you want to stories things so in this case the important lines are really the the 2 you up 1 up from the bottom says the Taliban package you want to install any time there's a script called might and you can install a series of scripts you could have different entry points into your application and just be 1 one-liners calling out to functions in my tool that um and these other options with with setuptools as well as with this this is technically the set of tools set up function uh you that that there such a thing called an entry point and where you can where you don't write a script your function is you entry point you tell set of tools what function but that what functions you want to be in 2010 what that you want the scripts to be called and when when somebody that installs it will install the scripts for you and that's the nice thing about that is on Windows it will create an executable program binary so looks much more like a native application to the user and they won't have got the why and the answer is so they take them more native on Unix as well X occurred
so this does not huge amount to
explodes and default Python doesn't sensible stuff for
you if you program just exits physical to the the end of the main function into the the descriptor that was running a nexus with 0 um and if it doesn't if you go uncaught exception elected with 1 or 2 but is not a great amount of information so if if you exited because the user provided by data you could provide a different exit code and if if you if you have network problems you to provide a different ExoCode there's any number of options you can just assume it's a very small token of information it's easy for somebody else to from standard have to pass on information they just have back number you should always consider the whole program is being run by another program and that's going make it more reusable making more useful um and is no specific standard for these numbers should be and continue Wikinews project recommendations they recommend that you set the high it if it's a serious error and I'm not really sure of any projects direction doing uh and on BST you have a 6 exits header file that lists a whole load of what they considered to be standard exit codes so it's probably a good place to look coming in doesn't have that if if there is a list of Mexico's things in Europe you have your except escaped exiting without condition it doesn't hurt to re use somebody else's standard 1st stuff I haven't had a chance to kill uh really uh online framework so people have been thinking about this a lot of the stuff here you can see it's kind of boilerplate code and it would be nice to somebody's handle stuff for you right where is configurations stored on Windows vs. Mac vs. Linux I think something both provide some subset of the and I haven't really worked with Cliff cliff is designed really for multi entry point projects and integrates set tools uh and Clint is really a whole bag stuff does colorization progress in command line passing and it'll load configuration from sensible options and it's a really cool library is not fantastically documented uh something planning to focus more half octave and planning to catch documentation to that's point students accepted um and I haven't covered cross-platform considerations so just kind of scheme that I want us to the configuration file locations and just like of time it's that there's a huge amount of this topic and I'm gonna write up to the isomers I think that would be useful to some people and so do have 5 minutes left I wasn't
infinity questions this is because if anybody has any questions it what is 1 of the creeks to get this command line programs to run faster especially if it's a short-lived command line programs by the full but obviously the same minimize you dependencies over the short-lived program I guess it's not really doing very much so you're looking into the start up time and kind of the time it takes to load and libraries say I'd say stick to the small of none of these APIs libraries none of them take a long time to load uh the yeah I I guess circulating library that has large data file
behind it was something like that but going to take some time but I mean the nice thing about poisonous as really fast sort of time anyway just 1 of the things that makes python such good language for writing command line tools because they often are sure this is isn't text on file but ignoring them and there is a so if you change the behavior when using terminal roses when you see applied how do you go about testing that because of sometimes if you program to change their behavior test environment can be very annoying times yeah uh this is uh um I just assume that I would talk would mark out the standard of and what standard error and object Query with 1 of the light about whether it's a TTY or not and then you can you can use it you can sort the results in a string i objects and this just checking against what you expect but do you have any recommendations for interactive programs on the command line I know this because question last time I said that I think you forgot to write slide opponents so uh I have used and this is briefly it can be a little bit awkward to get running on non Linux platforms but now I haven't done a huge amount of it so I can't really recommended specific and use the built-in modules the which you have created stuff like that but it gets kind of awkward once it gets very big uh yes but the earlier decades ago knowing the alternatives sits in these very cool module for anybody hasn't music uh so it's use and you for its recruits Europe shelf essentially on dedicated shelves it's you can prove you you allow it when you run your program you think of the blinking cursor and then you can then type type in commands so it's quite well programs with some commands for example if you don't have to type in the deep part of and each time you just you you within your contained environment and I use quite well with of course before so it you call you function is declared as being 1 of your commands and then anything else is on the command line just kind gets passed and was quite well with without past but it's you're right there that that kind of difficult to manage the manage just a question of trying to make sure that everything's extracted and during functions it doesn't just come from big mess becomes vise and event for a command line program who and so we also know that means basically that hosts walk right and so about testing of the the specific tools that you would recommend like you know cram for example comes to mind of of the tools that you would suggest for testing the common line parts of the application by tend to use unit tests um just because because we're dealing with text uh worries that data are not the kind of events and things like that financial unit test tends to have much the function of the One mean of this model to unit test as well I haven't used side of things but the robot if University of Surrey have you got a mark the some of the reasons also can get close loads of stuff so it can get cold so that would be the history of the universe and that 1 of the calls instead of of being right yeah that's going to you walking is always going to have that sort of thing is going that says the actual act on the command line he expects works well but I think that has to be a virus that I think we're out of time of flight I think
Gewichtete Summe
Schreiben <Datenverarbeitung>
Fahne <Mathematik>
Gruppe <Mathematik>
Radikal <Mathematik>
Skript <Programm>
Analytische Fortsetzung
Computerunterstützte Übersetzung
Explosion <Stochastik>
Folge <Mathematik>
Mathematische Logik
Interrupt <Informatik>
Äußere Algebra eines Moduls
Offene Menge
Wort <Informatik>
Prozess <Physik>
Radikal <Mathematik>
Familie <Mathematik>
Physikalischer Effekt
Sampler <Musikinstrument>
Arithmetisches Mittel
Spannweite <Stochastik>
Oktave <Mathematik>
Projektive Ebene
Extreme programming
Physikalische Theorie
Speicher <Informatik>
Bildgebendes Verfahren
Leistung <Physik>
Physikalisches System
Objekt <Kategorie>
Regulärer Ausdruck
System F
Streaming <Kommunikationstechnik>
Umkehrung <Mathematik>
Schreiben <Datenverarbeitung>
Kategorie <Mathematik>
Güte der Anpassung
Kontextbezogenes System
Dienst <Informatik>
Rechter Winkel
Lesen <Datenverarbeitung>
Klasse <Mathematik>
Automatische Handlungsplanung
Abgeschlossene Menge
Überlagerung <Mathematik>
Arithmetische Folge
Skript <Programm>
Inhalt <Mathematik>
Stochastische Abhängigkeit
Elektronische Publikation
Formale Sprache
Kartesische Koordinaten
Einheit <Mathematik>
Prozess <Informatik>
Funktion <Mathematik>
Freier Parameter
Funktion <Mathematik>
Ganze Zahl
Gewicht <Mathematik>
Interaktives Fernsehen
Framework <Informatik>
Elektronische Publikation
Einfache Genauigkeit
Automatische Differentiation


Formale Metadaten

Titel Writing Awesome Command-Line Programs in Python
Serientitel EuroPython 2014
Teil 89
Anzahl der Teile 120
Autor Smith, Mark
Lizenz CC-Namensnennung 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
DOI 10.5446/20000
Herausgeber EuroPython
Erscheinungsjahr 2014
Sprache Englisch
Produktionsort Berlin

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Mark Smith - Writing Awesome Command-Line Programs in Python Command-Line programs can have a lot to them - usually more than you think, yet often suffer from a lack of thought. This is a tour through how to structure your code, tools in the standard library and some 3rd party libraries. Take your command-line programs to the next level! ----- Python is a great language for writing command-line tools - which is why so much of Linux is secretly written in Python these days. Unfortunately, what starts as a simple script can quickly get out of hand as more features are added and more people start using it! The talk will consist of a tour through various useful libraries and practical code showing how each can be used, and include advice on how to best structure simple and complex command-line tools. Things to consider when writing command-line apps: * Single-file vs Multiple-file * Standard library only vs. 3rd party requirements * Installation - vs. native packaging The different parts of a command-line program: * Option Parsing: * Libraries: getopt, optparse, argparse, docopt * Sub-commands * Configuration: * Formats: Ini file, JSON, YAML * Where should it be stored (cross-platform); * Having multiple configuration files, and allowing user config to override global config * Output: * Colour - colorama * Formatting output for the user * Formatting output for other programs * How do you know when your output is being piped to another program? * Managing logging and verbosity * Managing streamed input * Exit values: What are the conventions? * Interactive apps - REPL * Structuring a bunch of programs/commands around a shared codebase. * Command-line frameworks: clint, compago & cliff * Testing command-line apps * Writing command-line tools in Python 3 vs Python 2
Schlagwörter EuroPython Conference
EP 2014
EuroPython 2014

Ähnliche Filme