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.
I've tried google but found nothing.
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)))
from os import system
from random import randrange
while True:
system('color %x' % randrange(256))
from x import. Python isn't friggin' Haskell.import os, system
while True:
os.system('color %s' % random.randrange(256))
from x import yfrom x import yfrom x import yfrom x import yfrom x import yfrom x import yfrom x import y
/prog/ pages 100-185 for BBCode information. Somebody has already done one of those./prog/ when I'm done with it.
I'm sorry to disappoint.
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 -> bsmodule 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 ">" ">" 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
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)