Status
Not open for further replies.
Delete duplicate lines anywhere

This regex matches a line if a duplicate of that line follows it, even if there are other lines between the matched line and the duplicate. Replacing all matches of this regex with nothing deletes all duplicate lines even if they are not adjacent.

PHP:
$replaced_data = preg_replace('/^(.*)(\r?\n\1)+$/m', '\1', $original_data);
 
Here's something trivial: generate random IDs using list comprehensions in Python :)

Code:
import random

source_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def random_id(length=7):
    return ''.join([random.choice(source_set) for i in range(length)])

EDIT: just realised, it can be used for generating salt as well.. silly me - why didn't I mention it before xD
 
Last edited:
YEAAAAAAAAAAH LIST COMPREHENSIONS!

Gaurav goes up in my list of cool coders. lol

Can do some really crazy stuff with list comps, dirty stuff [eviljmz]

Code:
import sqlite3
c = sqlite3.connect('test.db').cursor()
print '%s has the following animals:\n%s' % c.execute('SELECT :name, GROUP_CONCAT(y,\'\n\') FROM (SELECT (:sone || a.name || :stwo || a.species) as y FROM animals a, keepers k WHERE a.keeper_id = k.id AND k.name = :name) a;', {'sone' : 'Name: ', 'stwo' : ', Species: ', 'name' : raw_input('Keeper Name: ').capitalize()}).fetchone(), '%s' % str(c.close()).replace('None', '')

Prompts for a zoo keeper's name and outputs the animals they look after, in one line pretty much (apart from the initial import/connect).
 
That almost makes me spew as much as.

Code:
Public Function Adler32$(data As StringReader, Optional InitL& = 1, Optional InitH& = 0)
   With data
            Dim L&, h&
            h = InitH: L = InitL
               
               Dim StrCharPos&, tmpBuff$
               tmpBuff = StrConv(.mvardata, vbFromUnicode, LocaleID_ENG)
                  'The largest prime less than 2^16
                  L = (AscB(MidB$(tmpBuff, StrCharPos, 1)) + L) ' Mod 65521 '&HFFF1
                  h = (h + L) ' Mod 65521 '&HFFF1

                  If (0 = (StrCharPos Mod 5552)) Or _
                           (StrCharPos = Len(.mvardata)) Then
                     L = L Mod 65521  '&HFFF1
                     h = h Mod 65521  '&HFFF1
                     myDoEvents
                  End If
                  
               Next

      Adler32 = H16(h) & H16(L)
   End With
End Function

which by the way is just
Code:
static public uint adler32(byte[] data, int len){
 	        ulong a = 17;
 	        ulong b = 0;
 	        ulong MOD_ADLER = 65521;
 	        for(int i = 0; i < len; ++i){
 		        a = (a + data[i]) % MOD_ADLER;
 		        b = (b + a) % MOD_ADLER;
 	        }
 	        return (uint)((b << 16) | a);
         }
 
lol, well, that's a mess. Mine is just monstrous but if you split it up into separate statements (remove all the nesting and what not), it is well written :p
 
VB.NET snip:
Code:
Dim downloadcaptcha As New WebClient
downloadcaptcha.DownloadFile("http://rapidgator.net" & .ParseBetween(hr.Html, "id=""yw0"" src=""", """ alt="" />", "id=""yw0"" src=""".Length), CurDir() & "\captcha.png")
PictureBox1.Image = Image.FromFile(CurDir() & "\captcha.png")

a little piece of what I used to grab the captcha of rapidgator and put it in a picturebox. and btw I did not use web browsers for the other parts (I hate when people use those bloated pieces of shit).
 
PHP:
function rowcountGetter($tables, $puffix= '') {
	if ($puffix)
		$puffix = ' '.$puffix;
	($rows = sql::query('SELECT COUNT(*) FROM '.sql::$db->escape_string($tables).$puffix)) or sql::error(__FILE__,__LINE__);
	($alter = $rows->fetch_row()) or sql::error(__FILE__,__LINE__);
	$rows->free();
	return $alter[0];
}

Basically it counts the table in which rowcountGetter() is executed with
say i wanted to get post or threads of the database i'll do
PHP:
$forum_posts = rowcountGetter('posts');
how about if you need to get something off the database but need to classify where you can do
PHP:
$something = rowcountGetter('snippet', ' WHERE (`wj` & '.sql::search('post').')');
if there is room for improvement, i'd like to see :P
 
Seems interesting. Ideally though you should keep row counters in another table and update them on insertion of a new row. This way you simply select the appropriate counter rather than counting the rows each time (e.g. rowcounts(int key, int counter)).

My snippet, isn't a snippet today.
Rather a suggested logic you should implement.

Basically pagination. You reminded me of it just now.

Many, many people do the following:
- Select the rows (limited to rows_per_page from the page offset)
- Select count(*) of same query without a limit
- Use the count to display how many pages of results there are and to know how many page links to show

What people should do is this:
- Select the rows (limited to rows_per_page+1 from the page offset)
- If your result set is of size rows_per_page+1, display a 'next page' link and remove the last row from the set
- If your result set is of a size less than rows_per_page+1, show no 'next page' link
- If your current page is > 1, show a 'previous page' link

This way you never select a count(), you only use one query.
 
Wrote this for someone needing help:
PHP:
public static string[] grabProxy(string source)
        {
            var proxies = new List<string>();
            foreach (Match m in Regex.Matches(source, @"<td><span>([\s\S]*?)(\d+)</td>"))
            {
                string port = m.Groups[2].Value;
                string ip = m.Groups[1].Value;
                ip = Regex.Replace(ip, @"(<(div|span) style=""display:none"">\d+.*?>|<.*?>)", "").Trim();
                proxies.Add(string.Concat(ip, ":", port));
            }
            return proxies.ToArray();
        }
Parses out proxies from http://hidemyass.com/proxy-list/, hma being a ass trying to obfuscate proxies :P

Sample Usage:
PHP:
WebClient wc = new WebClient();

            string[] proxyList = grabProxy(wc.DownloadString("http://hidemyass.com/proxy-list/"));

            Array.ForEach(proxyList, s => Console.WriteLine(s));

            Console.ReadKey();
 
Seems interesting. Ideally though you should keep row counters in another table and update them on insertion of a new row. This way you simply select the appropriate counter rather than counting the rows each time (e.g. rowcounts(int key, int counter)).

My snippet, isn't a snippet today.
Rather a suggested logic you should implement.

Basically pagination. You reminded me of it just now.

Many, many people do the following:
- Select the rows (limited to rows_per_page from the page offset)
- Select count(*) of same query without a limit
- Use the count to display how many pages of results there are and to know how many page links to show

What people should do is this:
- Select the rows (limited to rows_per_page+1 from the page offset)
- If your result set is of size rows_per_page+1, display a 'next page' link and remove the last row from the set
- If your result set is of a size less than rows_per_page+1, show no 'next page' link
- If your current page is > 1, show a 'previous page' link

This way you never select a count(), you only use one query.
Ive seen that used before, its very efficient if you only need next & previous navigation. And great for innodb where count()'s are expensive.
 
Your wish is my command, nothing special just a excerpt of a rarely used but useful pattern

PHP:
abstract class System {
        abstract function toUrl();
	abstract function onReceived(Transaction $p);
	abstract function onSuccess();

// [...]
}
class UpstreamSystem extends System {
	private $u;
	function __construct($u){
		$this->u = $u;
	}
	
	function toUrl(){
		return $this->u->toUrl();
	}
	function onReceived(Transaction $p){
		return $this->u->onReceived($p);
	}
	function onSuccess(){
		return $this->u->onSuccess();
	}

Used to extend a class out of scope, usefull for callbacks etc.
 
PHP:
<?php

class Database
{
    var $con;
    var $sel;
    var $res;
    var $assoc;
    var $escape;
    var $array;
    var $row;

    function connect()
    {
        $this->con = mysql_connect("localhost", "host", "") or die(mysql_error());
        $this->sel = mysql_select_db("test") or die(mysql_error());
    }

    function close()
    {
        if (isset($this->connect)) {
            mysql_close($this->connect());
            unset($this->connect);
        }
    }

    function query($sql)
    {
        $this->res = mysql_query($sql) or die(mysql_error());
        return $this;
    }

    function fetch($type)
    {
        switch ($type) {
            case 1:
                $this->assoc = mysql_fetch_assoc($this->res);
                return $this;
                break;
            case 2:
                $this->array = mysql_fetch_array($this->res);
                return $this;
                break;
            case 3:
                $this->row = mysql_fetch_row($this->res);
                return $this;
                break;
        }
    }

    function escape($escape)
    {
        $this->escape = mysql_real_escape_string($escape);
        return $this->escape;
    }


    function get($num, $col)
    {
        switch ($num) {
            case 1:
                return ($this->assoc[$col]);
                break;
            case 2:
                return ($this->array[$col]);
                break;
            case 3:
                return ($this->row[$col]);
                break;
        }
    }

}
//initiate the database class
$database = new Database();
//connect to the database
$database->connect();
//run a query
$database->query("SELECT * FROM table");
//runs, chooses the type of query, and what collumn
$database->query()->fetch(1)->get(1,'username');
//close the database
$database->close();
?>
 
an abstract class i use to fill in objects properties from an array
PHP:
abstract class AbstractModel
{
    public function fromArray(array $data) {
        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->{$key} = $value;
            }
        }
    }
}
 
Seems interesting. Ideally though you should keep row counters in another table and update them on insertion of a new row. This way you simply select the appropriate counter rather than counting the rows each time (e.g. rowcounts(int key, int counter)).

My snippet, isn't a snippet today.
Rather a suggested logic you should implement.

Basically pagination. You reminded me of it just now.

Many, many people do the following:
- Select the rows (limited to rows_per_page from the page offset)
- Select count(*) of same query without a limit
- Use the count to display how many pages of results there are and to know how many page links to show

What people should do is this:
- Select the rows (limited to rows_per_page+1 from the page offset)
- If your result set is of size rows_per_page+1, display a 'next page' link and remove the last row from the set
- If your result set is of a size less than rows_per_page+1, show no 'next page' link
- If your current page is > 1, show a 'previous page' link

This way you never select a count(), you only use one query.

Pagination, you reminded me, oh, headache, I hate pagination. Thanks.
 
Status
Not open for further replies.
Back
Top