Updates from February, 2011 Toggle Comment Threads | Keyboard Shortcuts

  • diegor 6:06 pm on July 29, 2011 Permalink | Reply
    Tags:   

    HOWTO: rename all files in lowercase 

    Pretty simple.

    1. If you don’t have a *NIX operating system, please skip this post.
    2. Create a script with this content:
      #!/bin/sh
      for f in *; do
      g=`expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
      mv -n "$f" "$g"
      done

      and call it “rename.sh”

    3. Give it execution permissions, typing:
      chmod u+x rename.sh
    4. Execute the script from the directory where you have your files to rename
    If you have two files, “Foo” and “foo“, you’ll be notified and no files will be overwritten. So, don’t worry about that, you won’t lose any information. To be sure, try it with a few files in “/tmp” directory.
    Question? Suggestion? Comment! :)

     

     
    • Luca De Vitis 6:26 pm on July 29, 2011 Permalink | Reply

      ls -1 | while read file ; do mv -nv “${file}” “$(echo “${file}” | tr ‘[A-Z]‘ ‘[a-z]‘)” ; done

      • diegor 6:53 pm on July 30, 2011 Permalink | Reply

        Hi Luca, thanks for the trick.
        I tried but it doen’t work.. I have this:

        “namefile not overwritten”

        It works only if I drop “-n” option from mv command (too risky!!)

  • diegor 11:40 am on June 13, 2011 Permalink | Reply
    Tags:   

    HOWTO: using telnet to make HTTP request 

    Telnet is an old utility used in local network to offer a bidirectional communication text-oriented using a terminal. Can be used also to make an http call to a remote server with the purpose of testing. Let’s see how:

    1. Open you favorite terminal
    2. Type (instead of diegor.it you can choose whatever server you want)
      #> telnet diegor.it 80

      and you should have as output:

      Trying 75.119.192.123...
      Connected to diegor.it.
      Escape character is '^]'.
    3. Now type:
    4. GET / HTTP/1.1
      host: diegor.it
      <line feed>

      where “/” is the remote path. In this case we want the root. The output looks like this below:

      HTTP/1.1 200 OK
      Date: Mon, 13 Jun 2011 09:06:43 GMT
      Server: Apache
      Cache-Control: no-cache, must-revalidate, max-age=0
      Pragma: no-cache
      Expires: Wed, 11 Jan 1984 05:00:00 GMT
      Last-Modified: Sun, 12 Jun 2011 13:29:48 GMT
      Vary: Accept-Encoding
      Content-Length: 85589
      Content-Type: text/html;charset=UTF-8
      
      ...

    Pretty simple! Any questions or comments are welcome.

     
    • Paolo Bernardi 12:17 pm on June 13, 2011 Permalink | Reply

      Careful there, maybe that MaxOS X telnet is very smart, but usually the command is in the form “telnet hostname port”. So, ‘telnet http://www.diegor.it 80′ is a better way to do this, the protocol is specified by the port number. :-)

      • diegor 12:29 pm on June 13, 2011 Permalink | Reply

        I can’t get what are you saying. Where’s the “error”?

      • diegor 12:32 pm on June 13, 2011 Permalink | Reply

        Ok, now i get it. The problem is that this fucking wordpress put “http://” as prefix in www addresses

  • diegor 9:45 am on May 20, 2011 Permalink | Reply
    Tags:   

    HOWTO: “ip_conntrack: table full, dropping packet” 

    I used to manage a server based on linux and once I had this strange message:

    #> ip_conntrack: table full, dropping packet

    One of the effects of this message is that you aren’t able to make and receive new connections.
    To solve that is enough increase ip_conntrack_max value. As first step check the current value typing:

    #> cat /proc/sys/net/ipv4/ip_conntrack_max
    65536

    Now, increase this value typing:

    #> echo 131072 > /proc/sys/net/ipv4/ip_conntrack_max

    Generally the right value of ip_conntrack_max is set to the total MB of RAM multiplied by 16. If you have 8GB RAM (8192 MB), the value should be 131072.

     
  • diegor 10:27 pm on April 18, 2011 Permalink | Reply
    Tags:   

    HOWTO: passing arguments to functions in Django Template 

    Django doesn’t permit you because it goes against MVC model, but sometime it’s useful to get output (in view level) depending on specific arguments. For example in my model i have this method:

    def get_value(self, arg_to_pass):
       if (arg_to_pass.is_type_A):
          return self.value_for_A
       else:
          return self.value_for_B

    In your template you’ll type {{ object.get_value }} but you can’t pass argument. You can solve this issue with templatetags.
    Create a templatetag like this below:

    def callMethod(obj, methodName):
        method = getattr(obj, methodName)
    
        if obj.__dict__.has_key("__callArg"):
            ret = method(*obj.__callArg)
            del obj.__callArg
            return ret
        return method()
    
    def args(obj, arg):
        if not obj.__dict__.has_key("__callArg"):
            obj.__callArg = []
    
        obj.__callArg += [arg]
        return obj
    
    register.filter("call", callMethod)
    register.filter("args", args)

    Ok, almost done. Now, in your template, call your method typying:

    {{ object|args:arg_to_pass|call:"get_value" }}

    Job done! If you have any question, leave a comment! :)

    Source: sprklab.com

     
  • diegor 1:00 pm on March 11, 2011 Permalink | Reply
    Tags:   

    HOWTO: make vim show ending line spaces as characters 

    When you edit a file with vim, it’s useful understand where the line ends especially if there are spaces at the end of the line. Instead checking all lines, you can highlight white spaces. How?

    1. Open Vim from Terminal
    2. type
      :set list
    3. You see an output like the one below
    4. If you want see each space as character, type this command
      :set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
    5. The output should look like below

    Very simple and useful tip! :)

    If you have any doubt, advice or something else, comment this post! :)

    Source: Stackoverflow

     
    • proudlygeek 1:43 pm on March 11, 2011 Permalink | Reply

      Vim is my favourite text editor, i use it pretty anywhere :-)

      I’ll share a useful trick for commenting multiple lines of code:

      1) SHIFT-V to select multiple lines;
      2) Type “:s/^/#”

      Where “^” indicates the beginning of a line and “#” the comment symbol of your choice.

      To decomment a block of code:

      1) Shift-V
      2) Type “:s/^#//”

  • diegor 2:21 pm on February 26, 2011 Permalink | Reply
    Tags:   

    HOWTO: protect a web url with a password 

    Today I show you how to protect with a password a web url. This can be done in several ways (you can find it in Google) and in this post I use Apache2. Follow these steps:

    1. Install Apache2. Most OS have a preinstalled version (OSX has got)
    2. Create a file named “.htpasswd” via “htpasswd”:
      #> htpasswd -c /path/to/.htaccess diegor

      and when the prompt will ask you the password, type it (twice). The file looks like (in this case the password is like the username - DON’T DO IT, NEVER!!):

      diegor:HMAATyF2kQ37E
    3. Put this file to a directory NOT accessible to web server. For example, move it above www root directory
    4. Finally create a file named “.htaccess” in the directory to protect:
      AuthUserFile /path/to/your/safedir/.htpasswd
      AuthGroupFile /dev/null
      AuthName EnterPassword
      AuthType Basic
      
      require user diegor
    5. Restart Apache and go to you protected url via browser. You can’t access until you type your user and password

    That’s it. If you have any issue, comment this post.

     
  • diegor 9:48 am on February 7, 2011 Permalink | Reply
    Tags:   

    HOWTO: count lines of code 

    From today you will not go mental anymore to write a script that count lines of code of your last project. No, because there is “CLOC” that counts for you. It’s a huge (7640 lines) perl script that give you nice statistics about your project. You can find it on SourceForge.

    Once downloaded, unzip it and with a terminal cd (to cd=to change directory) to executable; then type the command below:

    #> perl cloc-1.53.pl /path/to/project/

    and you’ll have an output like this (it depends by the project’s complexity). The cool things is that recognizes languages and divides statistics depending on which have you used!

         671 text files.
         633 unique files.
          70 files ignored.
    
    http://cloc.sourceforge.net v 1.53  T=121.0 s (4.7 files/s, 1976.7 lines/s)
    -------------------------------------------------------------------------------
    Language                     files          blank        comment           code
    -------------------------------------------------------------------------------
    SQL                              3           1473           1257          85959
    Python                          97           5733           3435          29419
    Java                           170          10411          25782          28139
    HTML                           235           4833            200          28045
    Javascript                      32            803           1977           6994
    CSS                             21            473            149           3380
    Bourne Shell                    12             62            154            269
    Visual Basic                     1             46             50            136
    DOS Batch                        1              2              0              5
    -------------------------------------------------------------------------------
    SUM:                           572          23836          33004         182346
    -------------------------------------------------------------------------------

    That’s it! Oh yes, obviously you can take a look to its help launching it without any argument.

    And you, how many line of code have you written? :)

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel

Switch to our mobile site

Powered by Google Talk Widget