Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python Colours.

Name: Anonymous 2009-02-27 2:06

I'm just curious about how to print text in different colours with python..
I've tried google but found nothing.

Name: Anonymous 2009-02-27 2:08

ANSI escape codes.

Name: Anonymous 2009-02-27 2:11

interesting.. thanks

Name: Anonymous 2009-02-27 2:12

Forget it; it's NP-complete.

Name: Anonymous 2009-02-27 3:43

OP here

OK, quick life story:

I have been a gamer since I was a kid and I always wanted to learn how to make maps, mod, even program my own games, but never got around to it. About a week ago I installed python and now I'm hooked.

I've been on a break and even though I told myself I'd work the whole time, I've just been doing Python, and reading computer related stuff. I'm now obsessed with it! I was in wikipedia reading about ANSI escape codes because apparently thats what I need to colour text, and ended up clicking 9 different sublinks and learning a bunch of new computer stuff.

The point is I'm suddenly obsessed with computers. Even though I always spent lots of time on them I never worried too much about how they work now its all I think about.

Should I switch major to computer science? Or is it hard. Will I get disgusted by the fact that I have assignments and have to learn obselete stuff? I used to like physics and then school made me hate it. Are IT related jobs crap or should I go into it?

Name: Anonymous 2009-02-27 4:13

>>5
Start with this series of lectures: http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/

If you find that interesting, you should switch to CS. Otherwise, forget about it.

Name: Anonymous 2009-02-27 6:16


from os import system
from random import choice
colors = (1,2,3,4,5,6,7,8,9,0,'a','b','c','d','e','f')

while 1:
    system('color '+str(choice(colors))+str(choice(colors)))

Name: Anonymous 2009-02-27 6:21

>>7
Your code sucks and I hate you.

from os import system
from random import randrange

while True:
    system('color %x' % randrange(256))

Name: Anonymous 2009-02-27 7:06

>>6,7
Quit using from x import. Python isn't friggin' Haskell.

import os, system
while True:
    os.system('color %s' % random.randrange(256))

Name: Anonymous 2009-02-27 7:09

>>9
I'll import from x where I want, when I want.

from x import y
from x import y
from x import y
from x import y
from x import y
from x import y
from x import y

Name: Anonymous 2009-02-27 7:16

>>10
Perhaps I should also point out that >>9 is shorter than your shitty code.

Name: Anonymous 2009-02-27 7:22

Hey, hey! Don't breed FrozenVirgins in here, please!

Name: Anonymous 2009-02-27 7:39

>>9
Oh, fuck you, what's your problem? In Haskell we simply have [cpde]import <Module>[/code].

Name: Anonymous 2009-02-27 7:48

Oh, fuck you, shii. Just add the post edit feature.

Name: Anonymous 2009-02-27 8:32

>>14
And let all those BBCode failures get away?

Name: Anonymous 2009-02-27 9:40

>>9
I think you mean "Python isn't Modula-3." And if you do mean that, you're wrong anyway, because guess where GvR got the module syntax?

Name: Anonymous 2009-02-27 10:11

>>14
Use the preview button, sheesh.

Name: Anonymous 2009-02-27 10:21

>>11
It's also slower.

Name: Anonymous 2009-02-27 10:24

>>17
Yeah, that's another thing we need.

Name: Anonymous 2009-02-27 10:30

>>15
Testing my new BBCode editor.

ffff
ffff
f
f

f
f
f
f

Name: Anonymous 2009-02-27 10:51

Make a bbCode decompiler.

Name: Anonymous 2009-02-27 10:59

>>21
It appears that, unlike you, I actually spent a fair portion of my morning looking through /prog/ pages 100-185 for BBCode information. Somebody has already done one of those.

This is different, it's a C# interface that allows you to leverage turnkey functions for enterprise business BBCode. I'll probably shit this garbage all over /prog/ when I'm done with it.

Name: FrozenVoid 2009-02-27 11:06

Name: Anonymous 2009-02-27 11:07

>>22
Luckily, that will be never.

Name: Anonymous 2009-02-27 11:17

F(n) = φn−(1−φ)n
          √5


/r/ mathematical expression converter to bbcode

Name: Anonymous 2009-02-27 11:17

>>24
Actually, it's rather functional at the moment.

http://www.freeimagehosting.net/uploads/59e033776f.png

The BBCode parsing engine is extremely primitive though. I'll be porting ABBC to C# soon enough. Probably add hotkey support too. It will be finished relatively soon, I'm sorry to disappoint.

Name: Anonymous 2009-02-27 11:21

module Language.BBCode.Parser
   -- A fast monadic BBCode parser utilizing the industrial
   -- strength combinator library Parsec.
  (parseBBCode)
  where

import Language.BBCode
import Text.ParserCombinators.Parsec

bbcode = many (bbcodeElement <|> bbcodeText)
bbcodeElement = do element <- try bbcodeOpenElement
                   content <- bbcode
                   bbcodeCloseElement element
                   return (Element element content)
bbcodeOpenElement = do char '['
                       s <- many1 (oneOf "abcdefghijklmnopqrstuvwxyz#")
                       char ']'
                       return s
bbcodeCloseElement s = string ("[/" ++ s ++ "]")
bbcodeText = many1 (noneOf "[") >>= return . Text

parseBBCode bb = case parse bbcode "" bb of
                   Left  e  -> error $ show e
                   Right bs -> bs


module Language.BBCode (BBCode(..),
   -- Nest some BBCode to the specified depth.
   nest,
   -- Optimizes BBCode, by joining adjacent elements and text sections.
   optimize)
  where

import Data.List (groupBy)

data BBCode = Element String [BBCode]
            | Text String
  deriving Show

validBBCode = ["aa", "b", "code", "i", "m", "o", "s", "spoiler", "sub", "sup", "u"]

validateBBCode (Element e []) = e `elem` validBBCode
validateBBCode (Element e bs) = if e `elem` validBBCode
                                  then all validateBBCode bs
                                  else False
validateBBCode (Text _) = True

nest       _   0 final = final
nest element num final = [Element element (nest element (num - 1) final)]

optimize     [] = []
optimize bbcode =
    filter bbCodeFilter
  $ map bbCodeMerge
  $ groupBy bbCodeGroup
  $ map bbCodeOptimize bbcode
  where
    bbCodeGroup (Element a _) (Element b _) = a == b
    bbCodeGroup      (Text _)      (Text _) = True
    bbCodeGroup             _             _ = False

    bbCodeMerge      bs@((Text _):_) = Text (concatMap (\ (Text t) -> t) bs)
    bbCodeMerge bs@((Element e _):_) = Element e (concatMap (\ (Element _ es) -> es) bs)

    bbCodeFilter      (Text "") = False
    bbCodeFilter (Element _ []) = False
    bbCodeFilter              _ = True

    bbCodeOptimize (Text t) = (Text t)
    bbCodeOptimize (Element e es) = (Element e (optimize es))


module Language.BBCode.Disassemble (disassembleBBCode) where
import Language.BBCode
import Language.BBCode.Render
import Text.ParserCombinators.Parsec
import Data.List

html = many (try htmlHardBreak <|> try htmlBreak <|> htmlElement <|> htmlText)
htmlText = many1 (noneOf "<") >>= return . Text
htmlHardBreak = (string "<br clear=all>") >> (return (Element "br" []))
htmlBreak = (string "<br/>\n") >> (return (Text "\n"))
htmlElement = do
  (s, as) <- try htmlOpenElement
  content <- html
  htmlCloseElement s
  case lookup "class" as of
    Just c  -> return $ Element c content
    Nothing -> return $ Element s content
htmlOpenElement = do try $ char '<'
                     s <- many1 $ noneOf "/ >"
                     as <- manyTill (space >> spaces >> htmlAttribute) (char '>')
                     return (s, as)
htmlCloseElement s = do try $ string $ "</" ++ s ++ ">"
htmlAttribute = do key <- many1 $ noneOf "="
                   char '='
                   c <- oneOf "\"'"
                   value <- many $ noneOf (if c == '"' then "\"" else "'")
                   char c
                   return (key, value)

eval :: BBCode -> [BBCode]
eval (Element "a" x) = concatMap eval x
eval (Element "quote" x) = Text "> " : concatMap eval x
eval (Element s x) = (Element s (concatMap eval x)) : []
eval (Text x) = (Text (subst "&gt;" ">" x)) : []

subst _ _ [] = []
subst a b ax@(x:xs) = if isPrefixOf a ax
                        then b ++ (subst a b (drop (length a) ax))
                        else x : subst a b xs

disassembleBBCode bb = case parse html "" bb of
                         Left  e  -> error $ show e
                         Right bs -> concatMap eval bs

Name: Anonymous 2009-02-27 11:23

plot eq xs = (concat (replicate (maxy - miny) "[sub]")) ++ (plot' xs miny)
  where
    f x = 3 * (eq x)
    miny = round $ minimum (map f xs)
    maxy = round $ (maximum (map f xs)) + 2
    plot' [] currenty = (concat (replicate (maxy - currenty) "[/sub]"))
    plot' (x:xs) currenty =
      let nexty = round $ f x
          ydiff = abs $ currenty - nexty
      in if nexty > currenty
           then (concat (replicate ydiff "[/sub]")) ++ "." ++ (plot' xs nexty)
           else (concat (replicate ydiff "[sub]")) ++ "."  ++ (plot' xs nexty)

Name: Anonymous 2009-02-27 11:55

HULLO,

 IM 13 YEERS OLD AND I NO HASKELL, I AM SOOOO SMART

Name: Anonymous 2009-02-27 12:25

>>27
You're missing [#][#][/#][/#].

Name: Anonymous 2009-02-27 12:38

>>6
thnx alot

Name: Anonymous 2009-02-27 12:38

Make a BBCode parser that can handle http://sovietrussia.org/prog/read/19/10

Name: Anonymous 2009-02-27 13:27

>>30
br too, it's not quite compatible.

Name: Anonymous 2009-02-27 13:41

>>11
Don't forget that it's also broken, because you imported the wrong module.

Name: Anonymous 2009-02-27 14:03

[#]LOLWUT[/#]

Name: Anonymous 2009-02-27 14:12

>>34
It took 23 posts to notice that I slipped that in there.

Wow, /prog/. Just... wow.

Name: Anonymous 2009-02-27 14:14

>>35
No, you dicktard.

[b]why don't you go outside and play hide and go [i]fuck[/i] yourself?[/b]

That's what it's used for.

Name: Anonymous 2009-02-27 14:15

>>36
Yeah, except not. Back to /pr/

Name: Anonymous 2009-02-27 14:18

>>6
the lectures teach lisp... should I give up on python or should I learn both?

Name: Anonymous 2009-02-27 14:22

>>39
Use deductive reasoning.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List