omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Snippets - Access to back up

    Pythonista
    snippets
    6
    12
    6731
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Phuket2
      Phuket2 last edited by

      Does anyone know if there is a way to access the snippets saved in the preferences. I could not find anything in the forum about this. I also used the filenav app from @dgelessus to look around to see if I could see them stored in a folder but not luck. I am getting a little worried as I have lot of them now. I suspect at the moment if it's possible we will be to write a script to access them and back them up. Ideally, I would be nice to have a export and import function built into Pythonista. Depending upon the answers I get, I will write it up on the Pythonista Github page.

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        Snippets are stored as JSON in os.path.expanduser('~/../Snippets.json').

        Phuket2 1 Reply Last reply Reply Quote 2
        • Phuket2
          Phuket2 @omz last edited by

          @omz , thanks. Perfect

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp last edited by

            I really learn something every day I use Pythonista.
            Thanks to @Phuket2 , I even didn't know this feature!
            I love this app😘

            Phuket2 1 Reply Last reply Reply Quote 0
            • Phuket2
              Phuket2 @cvp last edited by

              @cvp, that's great. I also love it. Snippets are fantastic, I use them all the time. The way they come up in the autocomplete is brilliant. If you take your time and name your snippets to how you think, it's a breeze. I have one called ij_starter, if I just type in starter it pops up. But in that starter file I am ready to go with what I want typically to get going. You could also use the template feature for that also. I prefer the snippet way. But what's great about the snippets is they are copied in place. I have a lot of functions and classes as snippets. Anyway on a mobile system, reduces your typing by a hell of a lot.

              1 Reply Last reply Reply Quote 1
              • cvp
                cvp last edited by

                I just tried a snippet, only to understand how it functions.
                Shame on me, I see now how I have lost my time (which is free because I'm retired, but anyway 😬).
                I think I'll use this feature instead of templates.
                One more time, thanks to let me know.

                1 Reply Last reply Reply Quote 1
                • dgelessus
                  dgelessus last edited by

                  I haven't used snippets a lot personally, but I have two that I use quite often:

                  1. An ifmain snippet that expands to the standard if __name__ == "__main__" block
                  2. A doc snippet that just expands to an empty docstring """""" and places the cursor inside. This is useful in large files - when typing a docstring by hand, after the first three quotes, the syntax highlighting of the entire file is "inverted" (docstrings are code, and code is docstrings) which takes a while for the syntax highlighter to process. Then when the docstring is closed, the syntax highlighting is inverted again. In large files this can take quite a while, and with the snippet this doesn't happen because all six quotes are inserted at once.
                  Phuket2 1 Reply Last reply Reply Quote 0
                  • Phuket2
                    Phuket2 @dgelessus last edited by

                    @dgelessus , are you using and external keyboard? I only use the on screen keyboard. But really, I have a lot saved as snippets.

                    Eg. The below, I just save as ij_class_context, but have a lot of other class templates also. To be honest, the thing that makes this so powerful is the way it pops up in the editor as you are typing. The way @omz has implemented the recall/search of the snippets is fantastic. If you had to select your snippet from a ui it would a fraction as useful as it is now.

                    import ui,copy
                    
                    class MyClass(object):
                    	dict_list = []
                    	def __init__(self, *args, **kwargs):
                    		pass
                    		
                    	def __enter__(self):
                    		__class__.dict_list.append(copy.copy(self.__dict__))
                    		return self
                    	
                    	def __exit__(self, type, value, traceback):
                    		self.__dict__.update(__class__.dict_list.pop())
                    	
                    
                    if __name__ == '__main__':
                    	mc = MyClass()
                    	with mc as x:
                    		pass
                    
                    1 Reply Last reply Reply Quote 0
                    • dgelessus
                      dgelessus last edited by

                      Yes, I'm using an external keyboard. Snippets work exactly like normal completion suggestions here as well, so I can expand them with Tab. This is really convenient, I can type doc and then Tab, and I can start typing a docstring.

                      1 Reply Last reply Reply Quote 0
                      • Phuket2
                        Phuket2 last edited by

                        @dgelessus, ok. But my point was if you use an external keyboard, then the dynamics change. You will feel you can type fast enough on the external keyboard that snippets will not feel,as important to you as if we're using the on screen keyboard. Even though they as just as relevant.
                        But I am not complaining, you put out some great code. That matters more than how you got there

                        1 Reply Last reply Reply Quote 0
                        • upasaka
                          upasaka last edited by

                          Hi, am wondering where the snippets are kept in Editorial and whether it is possible to export or import snippets.

                          1 Reply Last reply Reply Quote 0
                          • abcabc
                            abcabc last edited by abcabc

                            Try this on editorial. You can do cut-and-paste. (Or you can write your own import/export based on this code.) May be there are better ways.

                            #coding: utf-8
                            import os, glob
                            
                            for f in glob.glob('../Snippets/*.snpt'):
                            	print('\n%s\n'%f + open(f).read() + '\n')
                            

                            Code printing snippet name instead of uuid

                            #coding: utf-8
                            import os, json, glob
                            
                            with open('../Snippets/Snippets.edcmd') as fp:
                            	json_obj = json.load(fp)
                            
                            snippet_dict = {}
                            for i in json_obj:
                            	snippet_dict[i['uuid']] = i['title']
                            
                            #print(snippet_dict)	
                            for f in glob.glob('../Snippets/*.snpt'):
                            	fuuid = (f.split('/')[-1]).split('.')[0]
                            	fname = snippet_dict[fuuid]
                            	with open(f) as fp:
                            		print('#####################')
                            		print('#'+fname+':')
                            		print(fp.read())
                            		print('#####################')
                            
                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors