diff --git a/_posts/2019-06-27-Python.md b/_posts/2019-06-27-Python.md new file mode 100644 index 0000000..bf00a3a --- /dev/null +++ b/_posts/2019-06-27-Python.md @@ -0,0 +1,117 @@ +--- +layout: post +title: "Hunting snakes with a shotgun" +description: "Python is a little too forgiving" +date: 2019-06-27 7:00:00 +categories: random +--- + +A rather large number of people know me as "the guy who does weird things with python". I would object to this title, but it is quite accurate. So, here are some of the things I like playing with in python. None of these are actually breaking the language, just little known facts and syntax. At some point I will share about actually breaking the language. For now, enjoy the weird things I have found over the past 6 years. + +## Type hints +A little known feature of python is called "type hinting" (PEP 484). This is actually quite common to see in standard libraries, and has it's own special syntax: +```python +# Here is a regular function +def meep(a, b): + return a*b^2 + +# This function has no real reason to exsist, and is lacking any sort of documentation. +# Let's add a docstring to explain what it does + +def meep(a, b): + """ This function returns the result of a times b squared """ + return a*b^2 + +# Ok. The docstring explains the function, but is not too helpful +# what are a and b? what does this return? +# For all we know, a could actually be a string (in which case, this function would return a string) +# Let's fix that up with a type hint + +def meep(a: int, b: int): + """ This function returns the result of a times b squared """ + return a*b^2 + +# Thanks to the :int (called a type hint in case you didn't notice that yet), we now know that this function expects two ints. +# Now, to finish this up with a secondary type hint to specify the return type +def meep(a: int, b: int) -> int: + """ This function returns the result of a times b squared """ + return a*b^2 + +# There. Now we can clearly see that this function takes too ints, and returns one int. +# If only this was a requirement in the language. So many headaches could be solved. +``` + +Now, keep in mind that this is called a type *hint*. The python compiler (yes.. Give me a second for that one) does not actually care if you obey the hint or not. Feel free to send incorrect data into a hinted function and see what you can break. Critical functions should both hint and check the data types being provided. + +## Type declarations +Just like type hints for functions, python has hints for variables too. +```python +# A regular variable. Must be declared with an initial value +my_state = None + +# my_state is None, as it has not been set, but needs to exist. +# Let's assume that my_state is to be a state: +class State: + status = False + def toggle(self): + self.status != self.status + +# Finally, its time to set the state to something useful +my_state = State() +my_state.toggle() + +# Ok.. I hate this. Let's start by using type declarations first +# Any variable can be un-initialized and just have a type. Like so: +my_state: State + +# This works for anything +is_alive: bool +age: int +name: str + +# Now, with this new knowledge, let's rewrite State +class State: + status: bool + def toggle(self: State) -> None: + self.status != self.status + +# And initialize my_state with slightly different syntax +my_state = State(status=True) +``` + +I have not found much use for this yet. Hopefully there is something cool to use it for. + +## One-line functions +This is more common knowlage. A function can be declared in one line +```python +# Here is an adder function +def adder1(a:int, b:int) -> int: + return a+b + +# Here is a one-line adder function +adder2 = lambda a,b : a+b + +# State from above can be compacted further: +class State: + status: bool + toggle = lambda self: self.status != self.status +``` + +## Ternary operations +On the trend of one-line code, We have the one-line if/else, also known as a Ternary in more sensible languages. +```python +# Here is an if/else +if 100 is 5: + print("The world has ended") +else: + print("All is good") + +# Here is a smaller if/else +print("The world has ended" if 100 is 5 else "All is good") +``` + +## Compiled python +This one is interesting. Python, like Java, is compiled into bytecode. So yes, it technically is a compiled language. To see said bytecode, take a look at any `.pyc` file sitting in your `__pycache__` + +## Blog formatting experiments +I am still playing with post formats, and various types of content. This is more random than I usually prefer. Let me know your thoughts on the social media platform of your choosing. \ No newline at end of file diff --git a/_site/feed.xml b/_site/feed.xml index a6d736f..3cd71f6 100644 --- a/_site/feed.xml +++ b/_site/feed.xml @@ -1,4 +1,4 @@ -Jekyll2019-06-26T16:54:25-04:00http://localhost:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.BashSmash2019-06-26T11:48:00-04:002019-06-26T11:48:00-04:00http://localhost:4000/random/2019/06/26/BashSmash<p>I was watching this great <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiOhNze_4fjAhUiB50JHR12D8AQwqsBMAB6BAgJEAQ&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6D1LnMj0Yt0&amp;usg=AOvVaw2nOgft0SoPZujc9js9Vxhx">Liveoverflow video</a> yesterday, and really liked the idea of building escape sequences with strings. So, I built a new tool, <a href="https://pypi.org/project/bashsmash/">BashSmash</a>.</p> +Jekyll2019-06-26T21:43:58-04:00http://localhost:4000/feed.xmlEvan PrattenComputer wizard, student, <a href="https://github.com/frc5024">@frc5024</a> programming team lead, and radio enthusiast.BashSmash2019-06-26T11:48:00-04:002019-06-26T11:48:00-04:00http://localhost:4000/random/2019/06/26/BashSmash<p>I was watching this great <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiOhNze_4fjAhUiB50JHR12D8AQwqsBMAB6BAgJEAQ&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6D1LnMj0Yt0&amp;usg=AOvVaw2nOgft0SoPZujc9js9Vxhx">Liveoverflow video</a> yesterday, and really liked the idea of building escape sequences with strings. So, I built a new tool, <a href="https://pypi.org/project/bashsmash/">BashSmash</a>.</p> <h2 id="the-goal">The goal</h2> <p>The goal of BashSmash is very similar to that described in Liveoverflow’s video. Do anything in bash without using any letters or numbers except <code class="highlighter-rouge">n</code> and <code class="highlighter-rouge">f</code> (he used <code class="highlighter-rouge">i</code> instead of <code class="highlighter-rouge">f</code>). This can both bypass shell injection filters, and generally mess with people.</p>